segmentation fault after fopen

This is a discussion on segmentation fault after fopen within the C Programming forums, part of the General Programming Boards category; hi, I get a segmentation fault after fopen. here is the code. Code: #include <stdlib.h> #include <stdio.h> #include <time.h> #include ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    2

    Arrow segmentation fault after fopen

    hi, I get a segmentation fault after fopen. here is the code.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    
    main()
    {
    
    FILE * fp;
    
     float v1,v2;
     float p;      
     float q;      
    
    
     printf("Input the probability of fragmentation v1:\n");
     scanf("%f",&v1);
     printf("Input another probability of EZ model v2:\n");
     scanf("%f",&v2);
     printf("Input the probability of infection p:\n");
     scanf("%f",&p);
     printf("%f Input the probability of recover q:\n",p);
     scanf("%f",&q);
    
     srand((unsigned) time(NULL));
    
     fp = fopen("data.txt","w");
    
    if(fp == NULL) {
    
            printf("dont exists\n");
    
        }
        else {
            printf("prints this line, crashes after here\n");
    
    	printf("doesnt get to this line");
        }
    
    printf("even less to this one");
    
     fprintf(fp,"v1=%10.5f v2=%10.5f p=%10.5f q=%10.5f\n",v1,v2,p,q);
    
    fclose(fp);
    }
    can some one help me, thanks!

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    You probably need to give the file name completely, including the directory, such as

    "C:\\mydir\\data.txt"

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Also, if you're printing messages for debug purposes, be sure to end them with a \n -- otherwise you won't see them, whether the code gets there or not.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Maybe you're missing "return; / exit();" after 1st printf() (if fp==NULL), and it crashes on fprintf()/fclose() but you don't see any output because there are no newlines ('\n') on other 2 printfs() so they get cached (not printed) and then you get SEGFAULT or whatever...

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Why not put the fprintf() and fclose() inside the else clause so that they never have a chance to get executed if the file could not be opened? Or, as rasta_freak suggested, put a "return 0;" or "exit(1);" inside the if clause if the file couldn't be opened.

    Quote Originally Posted by kcpilot View Post
    You probably need to give the file name completely, including the directory, such as

    "C:\\mydir\\data.txt"
    I always, always recommend against this. By leaving out a path, you use the current working directory, which is usually what you want anyway. It's more portable. You can move the program to a different location on the computer and it will still run, etc, etc.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Quote Originally Posted by tabstop View Post
    Also, if you're printing messages for debug purposes, be sure to end them with a \n -- otherwise you won't see them, whether the code gets there or not.
    Or call fflush().

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mingerso View Post
    Or call fflush().
    In fact, that is probably the most safe option. A newline on the end of the printf will most likely flush it, but it's not guaranteed. It is (almost) guaranteed that a non-newline ending will not be shown until some output produces a newline. fflush() is defined to force any pending output to be sent out at that point.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    2

    thanks everybody

    Thanks, all the above helped me. It works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. wierd segmentation fault with fopen
    By jaredw in forum C Programming
    Replies: 5
    Last Post: 08-27-2008, 07:43 PM
  3. fopen segmentation fault
    By fguy817817 in forum C Programming
    Replies: 27
    Last Post: 06-04-2008, 12:17 PM
  4. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 04:50 PM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM

Tags for this Thread


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