我正在使用这个:
if(strstr(inData, respond) != NULL)
在 inData
中搜索字符串 CMD
。
问题是当新数据看起来像这样时:
Reboot
CMD
strstr
不会找到它,因为它停止搜索新行所在的位置。
有没有一种优雅的方法(不使用花哨的函数,因为我不能)来克服它?
最佳答案
正如评论中指出的那样,strstr 不会在换行符边界处停止,而只会在字符串终止符 (\0) 处停止
这是手册页中的相关部分:
DESCRIPTION The strstr() function finds the first occurrence of the substring needle in the string haystack. The terminating null bytes ('\0') are not compared.
因此假设是,CMD 或 inData 之一中存在空字节。 您应该调查 this way 中的字符串 inData 和 CMD或 hexdump it (由@BLUEPIXY 建议)
关于c - 在包含新行的字符串中搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39817031/