My goal is to read a set of lines from a file and print out the lines containing a "keyword" along with their line number. I've managed to do so, but I'm unsatisfied with the code itself. I'm hoping someone with more experience than myself can help me come up with a more efficient and elegant solution. Please forgive my stickling :P.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
ifstream i{ "data.txt" };
string key_word{ "key" };
string line;
int line_counter{ 1 };
while (getline(i, line)) {
istringstream is{ line };
for (string s; is >> s;) { //search each word in the line for the keyword.
if (s == key_word) {
cout << line_counter << ": " << line << '\n';
}
}
line_counter++;
}