Processing a Log File

Hi All,
I am a newbie in C++. Lately I want to write a program to process the Logs which are in the following format

----some unwanted line---
----some unwanted line---
Enter:1
--Lines to be processed--
--Lines to be processed--
--Lines to be processed--
Exit
----some unwanted line---
----some unwanted line---
Enter:2
--Lines to be processed--
--Lines to be processed--
--Lines to be processed--
Exit
----some unwanted line---
----some unwanted line---

I want to process only the lines between Enter and Exit. Somehow I can manage toget to the line Enter. But my question is..
Is there a general way or an approach to solve this problem. How can I filter only the lines between the Enter and Exit removing all other unwanted lines??


Thank You
open file
keep getting one line each till not eof
see if it has enter
if yes then start processing the data
else keep reading the lines
if you are in enter state then see if its exit
if yes then stop processing the data
else keep processing the data.

lets write this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ifstream ifs("file path");
bool benter = false;
string buffer;
while(!ifs.eof())
{
readline(ifs,buffer);
if(benter)
{
if(buffer.find("Exit") == string::npos)
benter = false;
else
//process line
}
else
{
if(buffer.find("Enter") == string::npos)
benter = true;
}
}


it will be something like this... fine tune it.
If you know that you have to ignore a line, use yourfile.ignore()
http://www.cplusplus.com/reference/iostream/istream/ignore.html
In order to skip any line use as size argument numeric_limits<streamsize>::max()
http://www.cplusplus.com/reference/std/limits/numeric_limits.html
i have write it for you, it outputs in console the lines you wanted.


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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream file("C:\\log.txt", ios::in);
    string line;
	
    while(!file.eof())
    {
        getline(file, line);
		
	if(line.find("Enter") != -1)
	{
	    while(!file.eof())
	    {
	        getline(file, line);

		if(line.find("Exit") != -1)
			break;

		cout << line << endl;
	    }
	}
    }

    cout << "End of file reached" << endl;

    cin.get();
    return 0;
}
Topic archived. No new replies allowed.