Thread: Why do I have a segmentation fault ?

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    29

    Angry Why do I have a segmentation fault ?

    Hello I made an easy program which should copy a program ex.txt (containing 531) in a nex file, but when i launch it, it returns segmentation fault.

    Can you tell me what is wrong with my program ?

    thanks

    Code:
    # include <stdio.h>
    
    int main (void){
    int a;
    FILE *f1, *f2;
    
    f1 = fopen("~/Document/INF123/TEST/ex.txt", "r");
    f2 = fopen("~/Document/INF123/TEST/pr.txt", "w");
    
    fscanf(f1, "%d", &a);
    fprintf(f2, "%d", a);
    
    close(f1);
    close(f2);
    
    return 0;
    
    }

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by dekl View Post
    Code:
    close(f1);
    close(f2);
    close is a posix function taking a file descriptor, not a C file stream.
    Try fclose.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Apart from the close/fclose issue, fopen() returns NULL if it fails. You're not checking for that. If fopen() returns NULL for f1, you should not do fscanf(f1, ...). Similarly, if fopen() returns NULL for f2, you should not do fprintf(f2, ....)
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > f1 = fopen("~/Document/INF123/TEST/ex.txt", "r");
    Had you checked for success, and printed an error message on failure, you would have been greeted with "file not found".

    Things like "~" and "$HOME" are shell magic.
    Normal compiled C code doesn't know about such things.

    Either it has to be a full path (beginning with /), or a relative path to the current directory.
    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
    Dec 2011
    Posts
    51
    And do like me, read this, and read it again... and again.
    http://cboard.cprogramming.com/c-programming/88495-development-process.html

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    Thanks I forgot the f, but i still have the same message

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    Quote Originally Posted by grumpy View Post
    Apart from the close/fclose issue, fopen() returns NULL if it fails. You're not checking for that. If fopen() returns NULL for f1, you should not do fscanf(f1, ...). Similarly, if fopen() returns NULL for f2, you should not do fprintf(f2, ....)
    How do I check that?

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by dekl View Post
    How do I check that?
    Compare f1 and f2 to NULL ? A simple if statement.. " if(f1==NULL || f2==NULL)"

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    Quote Originally Posted by Salem View Post
    > f1 = fopen("~/Document/INF123/TEST/ex.txt", "r");
    Had you checked for success, and printed an error message on failure, you would have been greeted with "file not found".

    Things like "~" and "$HOME" are shell magic.
    Normal compiled C code doesn't know about such things.

    Either it has to be a full path (beginning with /), or a relative path to the current directory.
    That's it ! I just had to say the name of the file and not in shell...

    Thank you.

    and that you mariostg for that link!

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    29
    Oh ok I get it... I thought NULL would be error message, but I have to do it myself...

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by dekl View Post
    Oh ok I get it... I thought NULL would be error message, but I have to do it myself...
    You can get some information regarding the accurate error from errno.
    (I have no idea, how standard that is, though.)

  12. #12
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197

    Try some of the error testing functions for files

    ok try this
    Code:
    if ( fp = fopen( file.txt, "r") == NULL) 
    { 
    fprintf(stderr, "error opening file");
     }
     i
    f ( feof(fp)) 
    {
     fprintf(stderr, "Matching error occured in file"); 
    }
    then you try other error checking functions in file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By asherly7 in forum C Programming
    Replies: 3
    Last Post: 11-05-2010, 08:15 PM
  2. Segmentation Fault
    By tlman12 in forum C Programming
    Replies: 1
    Last Post: 10-29-2010, 03:57 PM
  3. Segmentation Fault
    By jat421 in forum C Programming
    Replies: 6
    Last Post: 04-03-2005, 02:26 PM
  4. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM