c++ - 请教C语言字符串和字符数组的比较问题
高洛峰
高洛峰 2017-04-17 14:45:56
0
2
693

用C语言读取1.txt文本文件:
文件内容如下:
12345
67890
98741

程序如下:

include <stdio.h>

include <string.h>

include <stdlib.h>

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函数呢?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
大家讲道理

What encoding is your 1.txt saved in?

阿神

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!