用C语言读取1.txt文本文件:
文件内容如下:
12345
67890
98741
程序如下:
int main(void)
{
FILE *fp = fopen("1.txt","r");
char *s="12345";
char buff[500];
while(fgets(buff,499,fp) != NULL)
{
if(strcmp(buff,s)==0) //为什么这里与“12345”比较没有等于0的时候呢
printf("%s",buff);
}
fclose(fp);
system("pause");
return 0;
}
//结果没有输出,应该怎么使用strcmp函数呢?
阿神2017-04-17 14:47:56
The data read by fgets will not help you remove the newline character 'n'. You will definitely not be able to compare it like this. The buff you read has one more newline character 'n' than your string s