New to C programming, what am i doing wrong?

This is a discussion on New to C programming, what am i doing wrong? within the C Programming forums, part of the General Programming Boards category; Im trying to make a basic open text file, read, write to text file program. However im new to c ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    3

    New to C programming, what am i doing wrong?

    Im trying to make a basic open text file, read, write to text file program. However im new to c and the program simply crashes when i run it. When i debug it crashes on the fwritef command

    Code:
    int main(int argc, char *argv[])
    {
            FILE *file = fopen( "test.txt", "r" );
            FILE *fp = fopen( "out.txt", "w");
    
            if ( file == 0 )
            {
                printf( "Could not open file\n" );
            }
            else
            {
                char x;
                while  ( ( x = fgetc( file ) ) != EOF )
                {
    
                fwritef(fp, x);
                }
                fclose( fp );
            }
            fclose( file );
    }
    i would appreciate if someone could point out what im doing wrong

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    1. what is fwritef() ?
    2. fgetc() returns an int, not char. Using the latter can cause EOF issues.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    Quote Originally Posted by zacs7 View Post
    1. what is fwritef() ?
    2. fgetc() returns an int, not char. Using the latter can cause EOF issues.
    sorry thats suppose to be fprintf, like i said im new and was screwing around to see if there was something called fwritef instead of fprintf

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    Nevermind, i fixed it. But thanks for your help anyways

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    You aren't checking if fp is NULL, that too can fail, perhaps for permission reasons.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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