468,512 Members | 1,415 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

A question about fscanf and feof !

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;
}

Oct 15 '06 #1
7 3536

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.

Oct 15 '06 #2
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.
Oct 15 '06 #3
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 ---
Oct 15 '06 #4
On Sun, 15 Oct 2006 10:47:13 -0400, Joe Wright
<jo********@comcast.netwrote:
<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
Nov 6 '06 #5
in 704529 20061106 083824 Dave Thompson <da*************@worldnet.att.netwrote:
>
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?
Nov 6 '06 #6
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).

Nov 6 '06 #7
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 ---
Nov 7 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by cylin | last post: by
2 posts views Thread by yezi | last post: by
7 posts views Thread by bhanuprakash | last post: by
2 posts views Thread by naija | last post: by
10 posts views Thread by Roman Zeilinger | last post: by
11 posts views Thread by mohdalibaig | last post: by
3 posts views Thread by Ranioo | last post: by
42 posts views Thread by Bill Cunningham | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.