Home  >  Q&A  >  body text

c++ - 请教C语言字符串和字符数组的比较问题

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

高洛峰高洛峰2765 days ago699

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 14:47:56

    What encoding is your 1.txt saved in?

    reply
    0
  • 阿神

    阿神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

    reply
    0
  • Cancelreply