A question about fscanf and feof !

Discussion in 'C Programming' started by ehui928, Oct 15, 2006.

  1. ehui928

    ehui928 Guest

    I use the following program to read some strings from an inupt file,
    and print them on the standard output.
    But the last string in the input file always printed twice, what is
    the reason and how can I make the last string be printed only once?
    Anyone give me some suggestions?

    input.txt:

    abc def ghi
    jk lm
    sea

    /* the string "sea" will be printed twice by the following program */
    #include <stdio.h>

    int main()
    {
    FILE *fp;
    char str[256];
    fp = fopen("input.txt", "r");
    if (fp == NULL)
    {
    perror("Read file error!\n");
    exit(1);
    }

    while (!feof(fp))
    {
    fscanf(fp, "%s", str);
    printf("%s ", str);
    }
    fclose(fp);
    return 0;
    }
    ehui928, Oct 15, 2006
    #1
    1. Advertising

  2. ehui928 wrote:
    > I use the following program to read some strings from an inupt file,
    > and print them on the standard output.
    > But the last string in the input file always printed twice, what is
    > the reason and how can I make the last string be printed only once?
    > Anyone give me some suggestions?
    >
    > input.txt:
    >
    > abc def ghi
    > jk lm
    > sea
    >
    > /* the string "sea" will be printed twice by the following program */
    > #include <stdio.h>
    >
    > int main()
    > {
    > FILE *fp;
    > char str[256];
    > fp = fopen("input.txt", "r");
    > if (fp == NULL)
    > {
    > perror("Read file error!\n");
    > exit(1);
    > }
    >
    > while (!feof(fp))
    > {
    > fscanf(fp, "%s", str);
    > printf("%s ", str);
    > }
    > fclose(fp);
    > return 0;
    > }


    See Question 12.2 at http://c-faq.com.
    Registered User, Oct 15, 2006
    #2
    1. Advertising

  3. ehui928

    ehui928 Guest

    Thank you !
    Now it works fine!

    Registered User 写é“:

    > ehui928 wrote:
    > > I use the following program to read some strings from an inupt file,
    > > and print them on the standard output.
    > > But the last string in the input file always printed twice, what is
    > > the reason and how can I make the last string be printed only once?
    > > Anyone give me some suggestions?
    > >
    > > input.txt:
    > >
    > > abc def ghi
    > > jk lm
    > > sea
    > >
    > > /* the string "sea" will be printed twice by the following program */
    > > #include <stdio.h>
    > >
    > > int main()
    > > {
    > > FILE *fp;
    > > char str[256];
    > > fp = fopen("input.txt", "r");
    > > if (fp == NULL)
    > > {
    > > perror("Read file error!\n");
    > > exit(1);
    > > }
    > >
    > > while (!feof(fp))
    > > {
    > > fscanf(fp, "%s", str);
    > > printf("%s ", str);
    > > }
    > > fclose(fp);
    > > return 0;
    > > }

    >
    > See Question 12.2 at http://c-faq.com.
    ehui928, Oct 15, 2006
    #3
  4. ehui928

    Joe Wright Guest

    ehui928 wrote:
    > I use the following program to read some strings from an inupt file,
    > and print them on the standard output.
    > But the last string in the input file always printed twice, what is
    > the reason and how can I make the last string be printed only once?
    > Anyone give me some suggestions?
    >
    > input.txt:
    >
    > abc def ghi
    > jk lm
    > sea
    >
    > /* the string "sea" will be printed twice by the following program */
    > #include <stdio.h>
    >
    > int main()
    > {
    > FILE *fp;
    > char str[256];
    > fp = fopen("input.txt", "r");
    > if (fp == NULL)
    > {
    > perror("Read file error!\n");
    > exit(1);
    > }
    >
    > while (!feof(fp))
    > {
    > fscanf(fp, "%s", str);
    > printf("%s ", str);
    > }
    > fclose(fp);
    > return 0;
    > }
    >

    First, the 'while (!feof(fp)) {}' is wrong. It would be fscanf which
    errors but you printf anyway. Then catch the error and quit.

    That is clearly wrong. Let me suggest something like..

    while (fscanf(fp, "%s", str))
    printf("%s ", str);

    At end-of-file, fscanf will return 0 and no printf takes place.

    P.S. I'd like to know where the 'while (!feof(fp)) {}' came from
    originally and why it keeps popping up.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Oct 15, 2006
    #4
  5. On Sun, 15 Oct 2006 10:47:13 -0400, Joe Wright
    <> wrote:
    <snip>
    > P.S. I'd like to know where the 'while (!feof(fp)) {}' came from
    > originally and why it keeps popping up.


    Originally, as the FAQ semi-sarcastically indicates, Pascal. And
    perhaps to an extent Ada, which provides BOTH the magic lookahead of
    Pascal and the error/signal behavior of PL/I and FORTRAN by default
    (and sort of by C, if you're careful about errorchecking).

    Why it continues, two decades after Pascal has practically vanished
    off the face of the earth*, is a harder question. Maybe Schildt? <G>

    (* Yes, I know there are still some users and (even) implementors. But
    you really have to look for them.)

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Nov 6, 2006
    #5
  6. ehui928

    Bob Martin Guest

    in 704529 20061106 083824 Dave Thompson <> wrote:

    >
    >Why it continues, two decades after Pascal has practically vanished
    >off the face of the earth*, is a harder question. Maybe Schildt? <G>
    >
    >(* Yes, I know there are still some users and (even) implementors. But
    >you really have to look for them.)
    >

    Really? Ever heard of Delphi?
    Bob Martin, Nov 6, 2006
    #6
  7. ehui928

    Old Wolf Guest

    Joe Wright wrote:
    > while (fscanf(fp, "%s", str))
    > printf("%s ", str);
    >
    > At end-of-file, fscanf will return 0 and no printf takes place.


    fscanf returns EOF if input fails before any conversion takes
    place, so this loop could run forever (and print the last string
    many times).
    Old Wolf, Nov 6, 2006
    #7
  8. ehui928

    Joe Wright Guest

    Old Wolf wrote:
    > Joe Wright wrote:
    >> while (fscanf(fp, "%s", str))
    >> printf("%s ", str);
    >>
    >> At end-of-file, fscanf will return 0 and no printf takes place.

    >
    > fscanf returns EOF if input fails before any conversion takes
    > place, so this loop could run forever (and print the last string
    > many times).
    >


    Thanks. Life is hopefully a learning experience. I have never used
    fscanf() in my whole long life. That I would write something that
    suggests my advice on how one might use it is clearly a brain fart. I
    sincerely apologize to all who have had to read this.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Nov 7, 2006
    #8
    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. Mantorok Redgormor

    feof usage

    Mantorok Redgormor, Sep 20, 2003, in forum: C Programming
    Replies:
    28
    Views:
    1,128
    Dave Thompson
    Sep 29, 2003
  2. question about gets() and feof().

    , Sep 16, 2005, in forum: C Programming
    Replies:
    3
    Views:
    347
    Simon Biber
    Sep 16, 2005
  3. rCs

    EOF vs. feof() and ferror()

    rCs, Oct 31, 2006, in forum: C Programming
    Replies:
    8
    Views:
    1,744
    Barry Schwarz
    Nov 1, 2006
  4. Willem

    Question on while(!feof(fp))

    Willem, Jan 30, 2008, in forum: C Programming
    Replies:
    5
    Views:
    377
    Keith Thompson
    Feb 1, 2008
  5. Andy
    Replies:
    3
    Views:
    3,351
Loading...

Share This Page