Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A question about fscanf and feof !

Reply
Thread Tools

A question about fscanf and feof !

 
 
ehui928
Guest
Posts: n/a
 
      10-15-2006
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;
}

 
Reply With Quote
 
 
 
 
Registered User
Guest
Posts: n/a
 
      10-15-2006

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.

 
Reply With Quote
 
 
 
 
ehui928
Guest
Posts: n/a
 
      10-15-2006
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.


 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      10-15-2006
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 ---
 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      11-06-2006
On Sun, 15 Oct 2006 10:47:13 -0400, Joe Wright
<(E-Mail Removed)> 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
 
Reply With Quote
 
Bob Martin
Guest
Posts: n/a
 
      11-06-2006
in 704529 20061106 083824 Dave Thompson <(E-Mail Removed)> 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?
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      11-06-2006
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).

 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      11-07-2006
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 ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie question: fgets() and feof() read last line twice Andy C Programming 3 03-19-2009 05:16 PM
Question on while(!feof(fp)) Willem C Programming 5 02-01-2008 02:22 AM
EOF vs. feof() and ferror() rCs C Programming 8 11-01-2006 04:45 PM
question about gets() and feof(). sunnyboyGuo@gmail.com C Programming 3 09-16-2005 05:36 PM
feof usage Mantorok Redgormor C Programming 28 09-29-2003 04:10 AM



Advertisments