简体   繁体   中英

C++ OBJ Parser - First Chance Exception

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.

我极力建议您使用freed,决不要使用fsanf及其变体。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM