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