Like Tree1Likes
  • 1 Post By stahta01

why is this program crashing.

This is a discussion on why is this program crashing. within the C Programming forums, part of the General Programming Boards category; 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); }...

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

    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
    384
    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
    384
    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
    2,553
    Because the file fails to be opened!

    Tim S.

    PS "\" needs to be escaped
    Salem likes this.

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, 08: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

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