I’m working on a program that takes in a Wavefront .obj file and eventually places it into OpenGL and displays the object.
Right now I’m just trying to read in a simple cube.obj file and print the contents. Its content is:
v -1.0 -1.0 -2.0 v 1.0 -1.0 -2.0 v 1.0 1.0 -2.0 v -1.0 1.0 -2.0 v -1.0 -1.0 -4.0 v 1.0 -1.0 -4.0 v 1.0 1.0 -4.0 v -1.0 1.0 -4.0 f 1 2 3 f 3 4 1 f 6 5 7 f 5 8 7 f 2 6 3 f 7 3 6 f 1 4 5 f 4 8 5 f 4 7 8 f 4 3 7 f 5 6 1 f 6 2 1
I placed the cube.obj in the command-line args and did a check to see if it opened.
FILE *fp;
fp = fopen(argv[1], "r");
if(fp == NULL)
fprintf(stderr, "cat: can't open %s\n", argv[1]);
else
printf("works");
cin.get();
It’s not even opening. Is there a better way to open the .obj file (as well as eventually print the contents of it?)
You have not provided enough information here, but it seems obvious that the problem is very likely the contents of
argv[1]. What does the pointer atargv[1]point to? Don’t assume, use your debugger and look at it. I bet it’s not what you think.You also don’t consider the fact that your program may not have been launched with any arguments, meaning that
argv[1]is reading past the end of a buffer. This is tangential though.EDIT:
Per your comment, you need to provide the full path of the file to
fopen. It should work if the file is in your running directory, but depending on your environemnt (VS is one) that may not be as simple as it sounds.I ran a test with a file
foo.txtin my \project\debug directory, the same directory as my executable. It failed to open the file with a relative path, but succeeded with the full path.However, if I simply double clicked the executable in that directory it worked. Apparently VS is doing something behind the scenes that is changing the working directory.
To use relative paths, right click the project -> Properties -> Debugging -> Working directory. Set that to your output folder and you should be fine.