编译成功后为什么得不出正确答案
#include"stdio.h"#include"stdlib.h"
#include"string.h"
int main(int argc,char *argv[])
{
char buf[128];
FILE *fp;
int line=0;
if(argc < 2)
{
perror("must have one argument\n");
exit(1);
}
if((fp=fopen(argv[1],"r+"))==NULL)
{
printf("the file is not exit:%s\n",argv[0]);
exit(1);
}
while(1)
{
if((fgets(buf,128,fp))!=NULL)
{
if(buf[strlen(buf)-1]=='\n')
{
line++;
}
}
if((fgets(buf,128,fp))==NULL)
{
break;
}
}
printf("the file is %d\n",line);
return 0;
}
编译后运行一个里面只有4行的文件,结果显示只有2行,不懂,求问哪里出错,各位