gpt4 book ai didi

裁剪字符串

转载 作者:太空宇宙 更新时间:2023-11-04 08:49:11 25 4
gpt4 key购买 nike

我有一个看起来像这样的缓冲区:

char *buffer;

缓冲区的内容如下所示:

apple; apple fruit \t data \n
apple tree \t data \n
apple pie \t data \n
Holland; Netherlands; The Netherlands '\t' data \n
....

我想搜索其中一个词。

我可以在缓冲区中找到单词。但是,如果我正在寻找苹果,我最终会从苹果到缓冲区的末尾,即苹果下面的所有行。它只是丢弃上面的所有内容。所以我想在点击\n 时在数据末尾切出。

我试过这个:

   // find the word.
char *found = strstr(buffer, word);

// try to keep the line containing the word and the data
// but discard everything after '\n'
found = strchar(found, '\n');
*(found +1) = 0;

printf(found);

然后当我尝试 printf 时,我根本不打印任何东西。所以当我运行 strchar 时,我毁掉了一切。如果我注释掉 strchar 部分,我可以看到我的单词和定义以及缓冲区中剩下的所有其他内容。

最佳答案

这是因为您更改 found 以指向您找到的换行符,这意味着 found 指向字符串中包含换行符和终止符。您需要为此使用不同的变量。

关于裁剪字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20280765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com