c++ 中是否有一个特定的函数可以返回我想要查找的特定字符串的行号?
ifstream fileInput;
int offset;
string line;
char* search = "a"; // test variable to search in file
// open file to search
fileInput.open(cfilename.c_str());
if(fileInput.is_open()) {
while(!fileInput.eof()) {
getline(fileInput, line);
if ((offset = line.find(search, 0)) != string::npos) {
cout << "found: " << search << endl;
}
}
fileInput.close();
}
else cout << "Unable to open file.";
我想在以下位置添加一些代码:
cout << "found: " << search << endl;
这将返回行号,后跟搜索的字符串。