I am trying to create a C++ program that will parse a .obj file and renders the model defined in the .obj file in OpenGL. So far, all this code is supposed to do is open an .obj file and put every vertex into a vector (vertices in .obj files are defined in a line starting with "v").
My full code is:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct vec3{
float x;
float y;
float z;
};
void loadOBJ(const char * Path){
vector<vec3> Vertices;
FILE * OBJFile;
vec3 temp = vec3();
fopen_s(&OBJFile, Path, "r");
char lineHeader[128];
//set to true when there are no more lines in the OBJ file
bool ended = false;
while(!ended){
fscanf_s(OBJFile, "%s", lineHeader);
if(strcmp(lineHeader,"v") == 0){
fscanf_s(OBJFile, "%f %f %f\n", &temp.x, &temp.y, &temp.z);
printf("Point: %f %f %f\n", temp.x, temp.y, temp.z);
Vertices.push_back(temp);
}else if(lineHeader != NULL){
fscanf_s(OBJFile, "\n");
}
else{
ended = true;
}
}
}
int main(){
loadOBJ("example.obj");
cin.get();
return 0;
}
The problem occurs with the line
fscanf_s(OBJFile, "%s", lineHeader);
If I comment this line out, I will not get the first chance exception. If I use a char instead of a string, I also do not get the first chance exception.
I highly recommend using freed and NEVER using fsanf and its variants.