検索したい特定の文字列の行番号を返すことができる特定の関数が 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;
これにより、行番号の後に検索された文字列が返されます。