strcmp 不工作

发布于 2024-07-23 20:57:53 字数 274 浏览 9 评论 0原文

我知道这可能是一个完全新手的问题(我已经很长一段时间没有接触过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

悲念泪 2024-07-30 20:57:53

您想要这样做:

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

翻了热茶 2024-07-30 20:57:53

fgets() 返回字符串“exit\n”——与 gets() 不同,它保留换行符。

fgets() is returning the string "exit\n" -- unlike gets(), it preserves newlines.

时光暖心i 2024-07-30 20:57:53

正如其他人所说,与 "exit" 进行比较失败,因为 fgets() 在缓冲区中包含换行符。 它的保证之一是缓冲区将以换行符结束,除非输入的行对于缓冲区来说太长,在这种情况下它不会以换行符结束。 fgets() 还保证缓冲区以 nul 终止,因此您不需要将 256 字节归零,而只需让 fgets() 使用 255 来获得该保证。

“exit\n”进行精确比较的简单答案要求用户没有意外在单词之前或之后添加空格。 如果您想强制用户小心使用 exit 命令,这可能并不重要,但通常可能会引起用户烦恼。

使用 strncmp() 可能允许 "exited""exit42" 等来匹配您可能不想要的位置。 这可能对您不利,特别是如果某些有效命令是其他有效命令的前缀字符串。

在一般情况下,将 I/O、标记化、解析和操作分成各自的阶段通常是一个好主意。

As others have said, comparing with "exit" is failing because fgets() 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 let fgets() 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.

小糖芽 2024-07-30 20:57:53

同意戴夫的观点。 您也可能希望使用 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/

虫児飞 2024-07-30 20:57:53

我建议您从字符串末尾删除 \n,如下所示。

char buf[256];
int len;
/* get the string, being sure to leave room for a null byte */
if ( fgets(buf,sizeof(buf) - 1) == EOF )
{
  printf("error\n");
  exit(1);
}
/* absolutely always null-terminate, the easy way */
buf[sizeof(buf) - 1] = '\0';
/* compute the length, and truncate the \n if any */
len = strlen(buf);
while ( len > 0 && buf[len - 1] == '\n' )
{
  buf[len - 1] = '\0';
  --len;
}

这样,如果您必须将输入的字符串与多个常量进行比较,则不必将 \n 添加到所有常量中。

I'd recommend that you strip the \n from the end of the string, like this.

char buf[256];
int len;
/* get the string, being sure to leave room for a null byte */
if ( fgets(buf,sizeof(buf) - 1) == EOF )
{
  printf("error\n");
  exit(1);
}
/* absolutely always null-terminate, the easy way */
buf[sizeof(buf) - 1] = '\0';
/* compute the length, and truncate the \n if any */
len = strlen(buf);
while ( len > 0 && buf[len - 1] == '\n' )
{
  buf[len - 1] = '\0';
  --len;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文