Like Tree1Likes
  • 1 Post By mariostg

Why do I have a segmentation fault ?

This is a discussion on Why do I have a segmentation fault ? within the C Programming forums, part of the General Programming Boards category; Hello I made an easy program which should copy a program ex.txt (containing 531) in a nex file, but when ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5,883
    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%.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    manasij7479 likes this.

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    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)"
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    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.)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187

    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, 02:29 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21