Thread: why is this program crashing.

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    why is this program crashing.

    Code:
    #include<stdio.h>
     
    int main()
    {
        FILE *fp;
        int i[2];
     
        fp=fopen("g:\carr","wb+");
         
        scanf("%d",&i[0]);
        scanf("%d",&i[1]);
        fwrite(i,sizeof(i),2,fp);
    }

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Got it...Sorry.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    1) You did not check if fp is NULL.

    2) i is a static array, so sizeof(i) is 2*sizeof(int). Your third arg to fwrite is 2, so that would be an "out of bounds" read.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    That is one of the reasons, and the other reason is that the file g:\carr was open with another program...

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Because the file fails to be opened!

    Tim S.

    PS "\" needs to be escaped

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help please...my program is crashing
    By odaetiep in forum C Programming
    Replies: 5
    Last Post: 11-19-2011, 09:04 PM
  2. program keeps crashing....
    By nospammax in forum C Programming
    Replies: 2
    Last Post: 05-07-2011, 12:11 AM
  3. Program keeps crashing
    By Charak in forum C Programming
    Replies: 5
    Last Post: 03-23-2011, 07:07 AM
  4. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  5. help plz crashing program
    By mill1000 in forum C++ Programming
    Replies: 3
    Last Post: 08-23-2002, 09:15 AM