c++ OBJ解析器-第一次机会异常

C++ OBJ Parser - First Chance Exception

本文关键字:第一次 机会 异常 OBJ c++      更新时间:2023-10-16

我正在尝试创建一个c++程序,该程序将解析。obj文件并在OpenGL中呈现。obj文件中定义的模型。到目前为止,所有这些代码应该做的就是打开一个.obj文件,并将每个顶点放入一个向量中(.obj文件中的顶点定义在以"v"开头的行中)。

我的完整代码是:

#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 %fn", &temp.x, &temp.y, &temp.z);
        printf("Point: %f %f %fn", 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;
}

这一行出现问题
     fscanf_s(OBJFile, "%s", lineHeader);

如果我注释掉这行,我将不会得到第一次异常。如果我使用字符而不是字符串,我也不会得到第一次机会异常。

我强烈建议使用free,永远不要使用fsanf及其变体。