Re: extra character when writing file

Discussion in 'C Programming' started by Bartc, Apr 10, 2008.

  1. Bartc

    Bartc Guest

    "Jim" <> wrote in message
    news:...
    > Hi There,
    >
    > I'm trying to read a file character by character. When I write the
    > file out, there is one extra character which shows on the screen as a
    > solid circle with a small question mark in the middle.
    >
    > Here is what I have:
    >
    > infile = fopen("encrypted.txt", "r");
    > outfile = fopen("plain.txt", "w");


    >
    > while(!feof(infile)) {


    That's the wrong way to use feof() as has been explained.

    I found it helpful myself to think of feof() as being named feof_triggered()
    instead. So it's not true until an attempt is made to read past EOF.

    I also made my own feof() which works the way you and I might expect, but
    that involves a lot of bad code using fseeks, ftells and assorted
    assumptions, so I won't post it here!

    > ch = fgetc(infile);


    ch returns an EOF value (usually -1) at the end of file. That's what you
    should be looking for. I revised your loop to:

    while(1)
    { ch=fgetc(infile);
    if (ch==EOF) break;
    fputc(ch, outfile);
    };

    --
    Bart
    Bartc, Apr 10, 2008
    #1
    1. Advertising

  2. Bartc

    Willem Guest

    Bartc wrote:
    ) I also made my own feof() which works the way you and I might expect, but
    ) that involves a lot of bad code using fseeks, ftells and assorted
    ) assumptions, so I won't post it here!

    What's wrong with using getc() and ungetc() ?

    int my_feof(FILE *stream) {
    int c = getc(stream);
    if (c == EOF) {
    if feof(stream) return F_EOF;
    if ferror(stream) return F_ERROR;
    return F_UNKNOWN;
    }
    if (ungetc(c, stream) == EOF) return F_UNKNOWN;
    return F_OK;
    }

    Which, I think, should work just dandy.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Apr 10, 2008
    #2
    1. Advertising

  3. Bartc

    Bartc Guest

    "Willem" <> wrote in message
    news:...
    > Bartc wrote:
    > ) I also made my own feof() which works the way you and I might expect,
    > but
    > ) that involves a lot of bad code using fseeks, ftells and assorted
    > ) assumptions, so I won't post it here!
    >
    > What's wrong with using getc() and ungetc() ?


    Well, I never thought of using ungetc(). I've just tried a simplified
    version of your code as:

    /* Return 1 (true) if positioned at EOF, or on error */
    /* Return 0 (false) otherwise */

    int pascal_eof(FILE *stream) {
    int c = getc(stream);
    if (c == EOF) return 1;
    if (ungetc(c,stream) == EOF) return 1;
    return 0;
    }

    --
    Bart
    Bartc, Apr 10, 2008
    #3
    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. HNguyen
    Replies:
    4
    Views:
    2,384
    HNguyen
    Dec 21, 2004
  2. Bob
    Replies:
    3
    Views:
    313
    Barry Schwarz
    Nov 28, 2005
  3. Shafik
    Replies:
    2
    Views:
    614
    Shafik
    Feb 6, 2007
  4. Spacebar265

    Scanning a file character by character

    Spacebar265, Feb 5, 2009, in forum: Python
    Replies:
    18
    Views:
    449
  5. mathieu
    Replies:
    3
    Views:
    582
    Bo Persson
    Sep 4, 2009
Loading...

Share This Page