Thread: Segmentation Fault. When trying to open file.

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    9

    Segmentation Fault. When trying to open file.

    I have a lengthy homework assignment to create a file parser and return tokens. It is 95% finished except this one part. I have two options from the beginning. The user presses 1 or 2. Pressing 1 opens an input and output file and is functioning properly. Pressing 2 opens an input file and this is where my segmentation fault is. I am only posting the Main because the program is 600 lines and it functions fine if I choose option 1. I am running linux mint and using geany as the ide. I tried running from the terminal as well.

    Code:
    int main()
    {    
        
        int response;
        char file1[20];
        char file2[20];
        
        printf("Press 1 to write tokens to a source file.\n");
        printf("Press 2 to check code for errors.\n");
        scanf("%d", &response);
        if(response == 1)        /*Parses file with user given input and output file*/
        {
            printf("Enter the source filename and the output file.\n");
            printf("Source code file: ");
            scanf("%s", file1);
            printf("Output file: ");
            scanf("%s", file2);
            fin = fopen(file1, "r");
            fout = fopen(file2, "wr");
            parser();
            if(error == FALSE)
            {
                printf("Parsing Finished Successfully"); /*Indicates Successful Compile*/
            }
            fclose(fin);
            fclose(fout);
        }
        else if(response == 2)            /*Parses file and outputs tokens to another file*/
        {
            printf("Enter the source code file name.\n");
            printf("Source code file: ");
            scanf("%s", file1);
            fin = fopen(file1, "r");
            parser();
            if(error == FALSE)
            {
                printf("Parsing Finished Successfully"); /*Indicates Successful Compile*/
            }
            fclose(fin);
        }
        else
        {
            printf("Error invalid selection. Enter 1 or 2.");
        }
        
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,055
    How does parser() know when only one file (fin) has been opened instead of two (fin and fout)?

    > fout = fopen(file2, "wr");
    Also, "wr" isn't a valid mode.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Fin and fout are declared globally if that answers the question. Fixed the "wr" in fopen also. I can post the whole program if you would like.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,055
    > Fin and fout are declared globally if that answers the question.
    Yes, I figured that much out.

    My question was "How does parse() know that fout has been closed on line 26".
    That is, you choose option 1, fin and fout get opened and closed.

    Then you choose option 2, and fin only is opened. Now, what is fout pointing to, and how does parse() know NOT to use it?

    Here, try this
    gcc -g prog.c
    gdb ./a.out
    ///
    (gdb) run


    At the inevitable crash, use the following commands
    bt - to display a stack trace
    frame n - to jump to a particular frame (pick the innermost function in your code)
    print var - to print variables.

    My guess is that you'll be pointing at a line of code using an old obsolete value of fout
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    Why the global variables? Yes post the smallest possible complete program that illustrates your problem. But first run your program through your debugger. The debugger should be able to tell you exactly where it detects the problem, and you should be able to view the variables at the time of the crash.

    Jim

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Ok got it. I don't know how I didnt see that before. I gotta remember those terminal commands. Thanks Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault reading and parsing data from a text file
    By deathseeker25 in forum C Programming
    Replies: 4
    Last Post: 05-19-2012, 12:33 PM
  2. Big structure + file pointer = segmentation fault
    By ERJuanca in forum C Programming
    Replies: 6
    Last Post: 03-02-2010, 05:46 PM
  3. Replies: 4
    Last Post: 05-05-2009, 05:35 AM
  4. Segmentation fault with a file pointer?
    By Matt13 in forum C Programming
    Replies: 14
    Last Post: 07-31-2004, 05:53 AM
  5. Segmentation fault with input test file
    By bentles in forum C Programming
    Replies: 20
    Last Post: 04-28-2002, 08:58 PM