不是的strstr功能 [英] strstr not functioning

查看:127
本文介绍了不是的strstr功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么的$ C $这种特殊件C上的strstr(),如果我输入测试?

Why does this particular piece of code return false on the strstr() if I input "test"?

char input[100];

int main()
{
    fgets(input, 100, stdin);
    printf("%s", input);

    if(strstr("test message", input))
    {
        printf("strstr true");

    }


}

我想搜索的strstr为第二个参数的情况下,第一个参数?它的工作原理,当我更换输入一些文字,或直接分配给它的东西,但它似乎不是用于fgets工作。

I thought strstr searched the first param for instances of the second param? It works when I replace input with some text or just assign it something directly, but it seems to not work with fgets.

推荐答案

这是因为与fgets存储换行符,所以当做的strstr失败的比较。

It's because fgets stores the newline character so when strstr does a comparison it fails.

从手册页:

与fgets()读取最多比流大小的字符一个都不能少
  并将它们存储到缓冲区由s指向。阅读后停止
  一个EOF或换行。如果一个新行被读取时,它被存储
         到缓冲器中。 A'\\ 0'被存储在缓冲器中的最后一个字符之后

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.

这篇关于不是的strstr功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆