0

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 3
    There's nothing wrong with getting a first chance exception, by itself, in the debugger. That said, the `_s` functions [require you to pass the size of the buffer](http://msdn.microsoft.com/en-us/library/w40768et.aspx), so... `fscanf_s(OBJFile, "%s", lineHeader, sizeof(lineHeader)/sizeof(char));` – T.C. Jun 25 '14 at 22:17
  • _'If I use a char instead of a string'_ What exactly do you mean? [Can you elaborate this in your question please](http://stackoverflow.com/posts/24419463/edit)? Did you mean it fails when trying to apply `fscanf_s()` to a `std::string` or a simple single `char` variable argument? – πάντα ῥεῖ Jun 25 '14 at 22:18

1 Answers1

-1

I highly recommend using freed and NEVER using fsanf and its variants.

user3344003
  • 20,574
  • 3
  • 26
  • 62