Re: Question about fopen, fput, fclose?

Discussion in 'C Programming' started by Peter Shaggy Haywood, Aug 30, 2003.

  1. Groovy hepcat RiRyoKu-Nightmare was jivin' on 25 Aug 2003 23:51:26
    -0700 in comp.lang.c.
    Question about fopen, fput, fclose?'s a cool scene! Dig it!

    >The following is source from a novice program that should open a file
    >and allow me to write to the file until <RETURN> is pressed but it
    >does not work on my


    I'm not surprised it doesn't work. It doesn't even compile. Oh, no,
    wait; yes it does (on C90), but with undefined behaviour. You're
    forgetting the argument for exit(), but you're also forgetting to
    include stdlib.h, so you don't have a declaration of exit(), and the
    compiler happily assumes that it returns int (which is wrong) and
    takes no arguments (which is also wrong). You don't even need exit()
    in this program. Just return instead.

    >#include <stdio.h>


    Add this, as it's needed for the definition of EXIT_FAILURE (and the
    declaration of exit(), if you use that function):

    #include <stdlib.h>

    >main()


    Replace the above line with this, since you want to write portable
    (to C99) code:

    int main(void)
    >{
    > FILE *fp;
    > char letter;


    Replace the above line with this, since you need to check for EOF:

    int letter;

    > puts("Type something and <PRESS ENTER> when finished to save.\n");
    > if((fp = fopen("MYFILE.TEXT","w"))==NULL)
    > {
    > puts("Cannot open the file\n");


    Replace the above line with this, because error messages belong on
    the standard error stream, stderr:

    fputs("Cannot open the file\n", stderr);

    > exit();


    Replace the above line with this, because it is more logical to
    return than call exit() from main() (but even if you do call exit(),
    you must pass it an argument), and EXIT_FAILURE is the standard
    error/failure indicating exit value:

    return EXIT_FAILURE;
    > }
    > do
    > {
    > letter=getchar();


    Insert the following, as you need to check for EOF, which would
    indicate an error reading the input:

    if(EOF == letter)
    (
    fputs("Error reading from standard input.\n", stderr);
    return EXIT_FAILURE;
    }

    > fputc(letter, fp);


    Insert the following, as you need to check for an error writing the
    output:

    if(ferror(fp))
    (
    fputs("Error writing to file.\n", stderr);
    return EXIT_FAILURE;
    }

    > }
    > while(letter!='\r');


    Replace the above line with this, because you need to check for a
    newline character (the character your program recieves when you press
    "enter" or "return"):

    while('\n' != letter);

    > fclose(fp);
    > return(0);
    >}


    Now your program should compile and run as expected.
    BTW, the parentheses around the return value are superfluous. return
    is not a function.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
    Peter Shaggy Haywood, Aug 30, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Trying_Harder

    fgets,fopen, fclose

    Trying_Harder, Aug 26, 2003, in forum: C Programming
    Replies:
    5
    Views:
    670
    John Bode
    Sep 3, 2003
  2. prama

    Re: Question about fopen, fput, fclose?

    prama, Aug 26, 2003, in forum: C Programming
    Replies:
    5
    Views:
    1,734
    Jack Klein
    Aug 28, 2003
  3. lihua
    Replies:
    19
    Views:
    909
    CBFalconer
    Jul 7, 2005
  4. David Mathog

    fclose then fopen equivalent for stdout?

    David Mathog, Oct 25, 2006, in forum: C Programming
    Replies:
    20
    Views:
    1,982
    Jordan Abel
    Oct 27, 2006
  5. kathy

    fopen and fclose?

    kathy, Feb 3, 2006, in forum: C++
    Replies:
    17
    Views:
    621
    Default User
    Feb 15, 2006
Loading...

Share This Page