There is a function to load files:
int loadfile(char *fn)
{
printf( "\n my path: '%s' \n", fn );
FILE *f = fopen( fn, "r");
printf( "got\n" );
...
return 1;
}
The first file is in main() newfile( argv[1] );, works. The second is get by flex parsing/reading the first file, what I belive isn’t related to the problem.
The console:
path: 'file1.hot'
got
path: 'file2.hot'
Segmentation fault: 11
The printf was able to print the char *fn, but fopen get a segmentation fault.
The next situation was, I tried explicit to put the file inside the loadfile doing fopen( "file2.hot", "r"); and works.
I’m compiling with g++, there is a different approaching when using c++ to use char * or fopen?
EDIT
Sorry, there’s no newfile( argv[1] );. Correct: loadfile( argv[1] );.
General remark: When using C++, please prefer
std::fstreamto the C-stylefopen/fread/etc.; also preferstd::stringtochar*. The latter alone will cure many memory headaches. If you, for some reason, have to stick to the C-style functions, be sure to check the return values (as mentioned in the other answers here already) –fopenfor example returns a NULL pointer when it fails.More specific to your question: Try to run your program under gdb (e.g.
gdb <your-program>), to see where the Segmentation fault occurs exactly; that way you will also be able to see more details (e.g. variable contents etc.). Alternatively, if working under linux, use analysis tools such as valgrind to detect any kind of memory access problems.