特定の文字列の行番号を返すことができる特定の関数がC++にありますか?検索された文字列が続く行番号を返しますC++特定の文字列のテキストファイルを検索し、その文字列がある行番号を返す
cout << "found: " << search << endl;
:
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.";
は、私はいくつかのコードを追加します。
ああ私はその単純なカウンタ方法を考えなかったと信じています。私はコールする特別な機能があると思っていました。ありがとう、私はそれを解決しました。 –
@JohnMarston:問題はありませんが、ループ状態を変更してください。それはあなたがエラー状態を処理することはできません。 –