strcmp 在使用 fgets 读取的行上
我正在尝试比较两个字符串。一个存储在文件中,另一个从用户 (stdin) 检索。
下面是一个示例程序:
int main()
{
char targetName[50];
fgets(targetName,50,stdin);
char aName[] = "bob";
printf("%d",strcmp(aName,targetName));
return 0;
}
在此程序中,当输入为 "bob"
时,strcmp
返回值 -1。 这是为什么呢?我认为他们应该是平等的。我怎样才能做到这一点?
I'm trying to compare two strings. One stored in a file, the other retrieved from the user (stdin).
Here is a sample program:
int main()
{
char targetName[50];
fgets(targetName,50,stdin);
char aName[] = "bob";
printf("%d",strcmp(aName,targetName));
return 0;
}
In this program, strcmp
returns a value of -1 when the input is "bob"
.
Why is this? I thought they should be equal. How can I get it so that they are?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
主要是因为在类unix系统下输入“\n”中存在行尾字符。
Mostly because of the end of line char in the input "\n" under unix like system.
strcmp
是少数几个具有 true 和 false 相反结果的函数之一...如果字符串相等,结果是 0,而不是您想象的 1...说到 < code>fgets,字符串末尾可能有一个换行符...您需要删除它...
要删除换行符,请执行以下操作。
注意:不要使用“strlen(aName) - 1”,因为 fgets 返回的行可能以 NUL 字符开头 - 因此缓冲区的索引变为 -1:
现在,strcmp 应返回 0 ...
strcmp
is one of the few functions that has the reverse results of true and false...if the strings are equal, the result is 0, not 1 as you would think....Speaking of
fgets
, there is a likelihood that there is a newline attached to the end of the string...you need to get rid of it...To get rid of the newline do this.
CAVEATS: Do not use "strlen(aName) - 1", because a line returned by fgets may start with the NUL character - thus the index into the buffer becomes -1:
Now,
strcmp
should return 0...fgets
会一直读取,直到看到换行符然后返回,因此当您在控制台中键入 bob 时,targetName
包含“bob\n”,与“bob”不匹配。来自 fgets 文档:(添加粗体)
在比较之前,您需要删除 targetName 末尾的换行符。
或将换行符添加到您的测试字符串中。
fgets
reads until it sees a newline then returns, so when you type bob, in the console,targetName
contains "bob\n" which doesn't match "bob".From the fgets documenation: (bolding added)
You need to remove the newline from the end of targetName before you compare.
or add the newline to your test string.
fgets 将
\n
附加到用户按 Enter 时从用户那里拉取的字符串。您可以通过使用strcspn
或仅将\n
添加到要比较的字符串末尾来解决此问题。这只是将
\n
替换为\0
,但如果你想偷懒,你可以这样做:但它并不那么优雅。
The fgets is appending a
\n
to the string that you are pulling in from the user when they hit Enter. You can get around this by usingstrcspn
or just adding\n
onto the end of your string you're trying to compare.This just replaces the
\n
with a\0
, but if you want to be lazy you can just do this:But it's not as elegant.
因为 fgets 将换行符嵌入到变量
targetName
中。这就失去了比较的意义。Because fgets is embededing the newline character into the variable
targetName
. This is throwing off the comparison.fgets
将换行符附加到字符串中,因此最终会得到bob\n\0
,它与bob\0
不同>。fgets
appends the newline to the string, so you'll end up withbob\n\0
which isn't the same asbob\0
.