Results 1 to 5 of 5

Thread: [C++] fopen() Segmentation Fault

  1. #1
    Join Date
    May 2008
    Beans
    1,029

    Question [C++] fopen() Segmentation Fault

    According to the documentation of 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?

    PHP Code:
    // 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;

    Quote Originally Posted by C++ Reference http://www.cplusplus.com

    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.
    Last edited by dodle; March 3rd, 2011 at 08:37 AM.

  2. #2
    Join Date
    Oct 2005
    Location
    Queensland, Australia
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: [C++] fopen() Segmentation Fault

    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) ?

  3. #3
    Join Date
    Jun 2007
    Location
    Canada
    Beans
    370

    Re: [C++] fopen() Segmentation Fault

    Quote Originally Posted by mdurham View Post
    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.
    GCS/O d+(-@) s: a-->? C(++) UL P+ L+++@ E@
    W++$ N++ !o K++ w(++) !O M(-) !V PS+(++)
    PE-() Y+ PGP++ t++(+++@)* 5++ X++@ R+++@
    tv+ b++(+++) DI++ D+ G+ e++>++++ h- r y?

  4. #4
    Join Date
    Feb 2007
    Location
    St. Louis, MO
    Beans
    4,930
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: [C++] fopen() Segmentation Fault

    This will work better for you:


    PHP Code:
    // Opening a binary file 

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

    int mainint argcchar *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
        
    fclosefile ); 
        return 
    0

    I have compiled this program ( g++ -o <program_name> <program_source> ) and it does indeed run.
    Last edited by stchman; March 3rd, 2011 at 09:46 AM.
    Windows, only good for gaming.

  5. #5
    Join Date
    May 2008
    Beans
    1,029

    Re: [C++] fopen() Segmentation Fault

    You are all right. I could have sworn the segfault was coming from fopen.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •