A question about fscanf and feof !

E

ehui928

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

Registered User

ehui928 said:
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.
 
E

ehui928

Thank you !
Now it works fine!

Registered User 写é“:
ehui928 said:
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.
 
J

Joe Wright

ehui928 said:
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.
 
D

Dave Thompson

On Sun, 15 Oct 2006 10:47:13 -0400, Joe Wright
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
 
B

Bob Martin

in 704529 20061106 083824 Dave Thompson said:
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?
 
O

Old Wolf

Joe said:
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).
 
J

Joe Wright

Old said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,832
Latest member
UtaHetrick

Latest Threads

Top