PDA

View Full Version : [SOLVED] [C++] fopen() Segmentation Fault



dodle
March 3rd, 2011, 08:09 AM
According to the documentation of fopen (http://www.cplusplus.com/reference/clibrary/cstdio/fopen/) if it cannot open a file a NULL pointer is returned. But my program crashes with a segmentation fault if I try to open a file that doesn't exist. Shouldn't it just return the NULL pointer? Do I have to test for the file's existence before I can use fopen?


// Opening a binary file

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
FILE *file;
if (file = fopen("dogs", "rb")) // Crashes here because "dogs" does not exist
{
cout << "Opened file\n";
fclose(file);
return 0;
}

cout << "Failed to open file\n";
fclose(file);
return 1;
}



Return Value

If the file has been succesfully opened the function will return a pointer to a FILE object that is used to identify the stream on all further operations involving it. Otherwise, a null pointer is returned.


SOLVED: Okay, I see in the documentation that when using the "r" mode in fopen the file must exist:


Mode:
"r" | Open a file for reading. The file must exist.

mdurham
March 3rd, 2011, 08:47 AM
SOLVED: Okay, I see in the documentation that when using the "r" mode in fopen the file must exist:

Which documentation says that it 'must' exist? I think if the file doesn't exist fopen() will fail. Could the problem be that you are trying to fclose(NULL) ?

ve4cib
March 3rd, 2011, 09:04 AM
Which documentation says that it 'must' exist? I think if the file doesn't exist fopen() will fail. Could the problem be that you are trying to fclose(NULL) ?

If you are opening a file for reading and the file does not exist fopen will fail (return NULL). If you are opening a file for writing then the file will be created if it does not exist. Different behaviour for different modes.

stchman
March 3rd, 2011, 09:34 AM
This will work better for you:




// Opening a binary file

#include <iostream>
#include <stdio.h>
using namespace std;

int main( int argc, char *argv[] )
{
FILE *file;
if( ( file = fopen( "dogs", "rb" ) ) == NULL ) // checks to see if file dogs exists
{
cout << "File not found\n";

// exit the program
return 1;
}
else
{
cout << "File opened\n";
}

// close the file
fclose( file );
return 0;
}
I have compiled this program ( g++ -o <program_name> <program_source> ) and it does indeed run.

dodle
March 3rd, 2011, 10:20 AM
You are all right. I could have sworn the segfault was coming from fopen.