与if语句中的字符串比较不起作用 [英] Compare with string in if statement doesn't work
本文介绍了与if语句中的字符串比较不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试比较从scanf和fscanf获得的两个字符串。我已经弄明白每个变量里面的内容是什么。它都显示相同的字符串,但在if语句中与这两个字符串进行比较后,它不起作用并执行else语句。我的代码出了什么问题?
I try to compare my two strings that obtained from my scanf and fscanf. I already figured out what is the content inside the each variable. It both show the same strings, but after I compare with this two string in if statement, it doesn't work and execute else statement instead. What's wrong with my codes ?
int main(void)
{
...
char input[256];
printf("Enter your name: ");
scanf("%s",&input);
fp = fopen(_FILE,"r+");
for(i=0;i<textlines(_FILE);i++)
{
fscanf(fp,"%s %d",stuff[i].name,&stuff[i].salary);
if(input == stuff[i].name)
{
// Update name here
}
else
{
printf("Not Found");
}
}
return 0;
}
推荐答案
==
只检查指针是否相等。使用 strcmp
而不是
==
just checks for pointer equality. Use strcmp
instead
这篇关于与if语句中的字符串比较不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文