strcmp 不工作
我知道这可能是一个完全新手的问题(我已经很长一段时间没有接触过 C 了),但是有人可以告诉我为什么这不起作用吗?
printf("Enter command: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
if (strcmp(buffer, "exit") == 0)
return 0;
如果我输入“exit”,它不会输入if,这与“buffer”的长度有关吗?
有什么建议么?
I know this may be a totally newbie question (I haven't touched C in a long while), but can someone tell me why this isn't working?
printf("Enter command: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
if (strcmp(buffer, "exit") == 0)
return 0;
If I enter "exit" it doesn't enter the if, does it have to do with the length of "buffer"?
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您想要这样做:
strcmp(buffer, "exit\n")
也就是说,当您输入字符串并按“enter”键时,换行符将成为
buffer
的一部分>。或者,使用 strncmp(),它只比较字符串的 n 个字符
You want to do this:
strcmp(buffer, "exit\n")
That is, when you enter your string and press "enter", the newline becomes a part of
buffer
.Alternately, use strncmp(), which only compares n characters of the string
fgets() 返回字符串“exit\n”——与 gets() 不同,它保留换行符。
fgets() is returning the string "exit\n" -- unlike gets(), it preserves newlines.
正如其他人所说,与
"exit"
进行比较失败,因为fgets()
在缓冲区中包含换行符。 它的保证之一是缓冲区将以换行符结束,除非输入的行对于缓冲区来说太长,在这种情况下它不会以换行符结束。fgets()
还保证缓冲区以 nul 终止,因此您不需要将 256 字节归零,而只需让fgets()
使用 255 来获得该保证。与
“exit\n”
进行精确比较的简单答案要求用户没有意外在单词之前或之后添加空格。 如果您想强制用户小心使用 exit 命令,这可能并不重要,但通常可能会引起用户烦恼。使用
strncmp()
可能允许"exited"
、"exit42"
等来匹配您可能不想要的位置。 这可能对您不利,特别是如果某些有效命令是其他有效命令的前缀字符串。在一般情况下,将 I/O、标记化、解析和操作分成各自的阶段通常是一个好主意。
As others have said, comparing with
"exit"
is failing becausefgets()
included the newline in the buffer. One of its guarantees is that the buffer will end with a newline, unless the entered line is too long for the buffer, in which case it does not end with a newline.fgets()
also guarantee that the buffer is nul terminated, so you don't need to zero 256 bytes but only letfgets()
use 255 to get that guarantee.The easy answer of comparing to exactly
"exit\n"
required that the user did not accidentally add whitespace before or after the word. That may not matter if you want to force the user to be careful with the exit command, but might be a source of user annoyance in general.Using
strncmp()
potentially allows"exited"
,"exit42"
, and more to match where you might not want them. That might work against you, especially if some valid commands are prefix strings of other valid commands.In the general case, it is often a good idea to separate I/O, tokenization, parsing, and action into their own phases.
同意戴夫的观点。 您也可能希望使用 strncmp() 代替。 然后您可以设置比较的长度。
http://www.cplusplus.com/reference/clibrary/cstdio/fgets/< /a>
http://www.cplusplus.com/reference/clibrary/cstring/ strncmp/
Agree with Dave. Also you may wish to use strncmp() instead. Then you can set a length for the comparison.
http://www.cplusplus.com/reference/clibrary/cstdio/fgets/
http://www.cplusplus.com/reference/clibrary/cstring/strncmp/
我建议您从字符串末尾删除 \n,如下所示。
这样,如果您必须将输入的字符串与多个常量进行比较,则不必将 \n 添加到所有常量中。
I'd recommend that you strip the \n from the end of the string, like this.
That way, if you have to compare the inputted string against several constants, you're not having to add the \n to all of them.