C++ 如何在文件搜素指定的字符串
使用C++打开一个文本文文件,并读取内容,在读取的内容中如何判断是否包含指定的字符串。
e.g:char chr[128];
fstream file;
file.open("a.txt",ios::in);
if(file.is_open())
{ while (!file.eof())
{
getline(chr,128);
}
}
如果a.txt的内容为:123456good
147258bad
我该如何判断哪行中包含 good 。
求具体代码。
[解决办法]
读到string里,然后用find查找
[解决办法]
strstr()
Find one string inside another
Synopsis:
#include <string.h>
char* strstr(char* str,
char* substr );
[解决办法]
或者自己写:
遍历一下字符串,调用strncmp比较指定长度字符串
[解决办法]
std::string temp = chr;
if( -1 != string.find("good") ) ///>包含good
[解决办法]
如果是string类型就用find或substr函数,是char*类型用strstr函数都是查找字符串