S
sunnyboyGuo
hello everyone,
my code is like this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
char str[128];
if((fp=fopen(argv[1], "r"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
while(!feof(fp)) {
if(fgets(str, 126, fp))
printf("%s", str);
else
printf("error.\n");
}
fclose(fp);
return 0;
}
////////////////////////////////////////////
if the input file is like this:
abc //first line
ddd //second line. and there is no "enter" in the end.
our code will run correctly.
if the input file is like this:
abc //first line
ddd //second line. in the end, press "enter"
//last line, this line is press "enter" in the second line.
//and there is none other key in the last line.
in this case, my code will output "error".
what's wrong?
sincerely
guo
my code is like this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
char str[128];
if((fp=fopen(argv[1], "r"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
while(!feof(fp)) {
if(fgets(str, 126, fp))
printf("%s", str);
else
printf("error.\n");
}
fclose(fp);
return 0;
}
////////////////////////////////////////////
if the input file is like this:
abc //first line
ddd //second line. and there is no "enter" in the end.
our code will run correctly.
if the input file is like this:
abc //first line
ddd //second line. in the end, press "enter"
//last line, this line is press "enter" in the second line.
//and there is none other key in the last line.
in this case, my code will output "error".
what's wrong?
sincerely
guo