New to fstream/argc and argv, is not displaying results ? (Grep program)

I'm currently creating a program that will allow a user to look for a word inside of an input file, then print the whole line in which the program found that word into an output file.
I'm VERY new to C++, and very bad at it admittedly.

The user enters this at the cmd prompt, when calling the .exe with 3 args

For ex: C:\grep.exe "IF" test.txt output.txt

where "IF" would be the keyword, test is the input file, and output would be the new output file.

I'm currently stuck on the very first part, I can't even get it to show the line the results are on with cout, it displays the whole file. (I haven't even tackled writing it to an output file.. yikes)
Can someone help me find out how to display the line the keyword would be on? This is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char* argv[]) {
	string word = argv[1];
	string ifile = argv[2];
	string ofile = argv[3];
	int count = 0;
	string line;
	char ch;

	// input
	ifstream in;
	in.open(ifile);

	/* output
	ofstream out;
	out.open(ofile);	
	*/

	// work
	if( in.is_open() ) {
		while( !in.eof() ) {

			getline(in, line);
			cout << line << endl;;


		/*
				while( getline(in, line) ) {
			cout << line << endl;;
			
			if( ifile.find(word) ) {
				while( in.get(ch) ) {
					out.put( ch );
				}
			}
		}
		*/
		}
		in.close();
	}
	
	//out.close();
}


Any help is much appreciated!
because

1
2
3
4
while( !in.eof() ) {
	getline(in, line);
	cout << line << endl;;
}


is exactly doing that :)
while the end of file is not reached, getline and print it on screen...

fyi ";;" is not necessary ";" will do the job ;)

regards
Last edited on
Topic archived. No new replies allowed.