Outputting specific contents of a file to another file

I'm doing an assignment for school which is to simulate the grep command in C++. We are to take in a word to search, an input file to search through, and output file to output the results. The only text that should be in the output file is the contents of each line that had the search word in it, with a line number of where it was in the input file. We are supposed to use the functions of <fstream>.

I'm having issues outputting just the line the word appeared on. I can't seem to get my head around it.

What I have so far for outputting:
1
2
3
4
5
6
7
8
9
10
11
	while( getline( in,line ) ) {
		if( line.find( keyword ) ){
			while(!in.fail() && !in.eof() && line.find( keyword )) {
				char c;
				in.get( c );
				out.put( c );
				
			 }
			++keywordCount;
		}
	}


That skips the first line the word appeared on and just outputs the rest of the contents of the input file.

Any suggestions on how to go about doing this? Any help is appreciated.
What is that while loops supposed to do. line never changes in it, so the third condition is useless. Of course it will copy the rest of the file. What you need to do is simply out << linenumber << ' ' << line << '\n';
Topic archived. No new replies allowed.