Live Console Logging

I have a log file that is constantly updated every several milliseconds. It contains lines that either start with a 1 or a 0. I am trying to read it from the beginning and cout the lines that start with a 1 to the console. Then once I am caught to the current time, just listen for a file update and cout new lines as they come in. Each time I reach the end of the file, my program throws an error instead of waiting for the next line to be logged. How can I wait for new updates?

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
//#include <stdafx.h>
#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

int OpenFile()
{ 
	fstream SampleFile; 
	SampleFile.open ("sample.txt"); 
	//Open the File
	if (SampleFile.is_open()) 
	{ 
		//Create a single character to quit when ready.
		string line="test"; 
		while(line.empty()==false)
		{ 
			//Get lines one by one and only cout those where first character is ==1.
			getline(SampleFile,line);
			char ch=line.at(0);
			if (ch=='1')
			{ 
				cout<<line<<endl; 
			}
		} 
		SampleFile.close(); 		
	} 
	else 
		cout <<"Could not open file."<<endl; 
	return 0; 
} 

int main() 
{ 
	OpenFile(); 
	return 0; 
}
1
2
getline(SampleFile,line); //last read reads nothing
char ch=line.at(0); //Trying to access nonexisting symbol 
What is the correct way to do it?
1
2
3
4
while( getline(SampleFile,line) )
{
//...
}
Can you show all the code? It sounds like I need to delete some things.
1
2
3
4
5
6
7
while(getline(SampleFile,line))
{ 
	if (line.at(0) == '1')
	{ 
		cout<<line<<endl; 
	}
} 
Thanks so much for that. But the file is being written too. As soon as I run the program, it reads all the way to the last line and gives "Press any key to continue." I need it to continue to print out the upcoming lines that are being logged. That's the real issue. Any ideas?

Open file. Read till the end. Remember end position. Close file.
Wait.
Open file. Seek to the remembered position. Read till the end... etc.
Last edited on
Topic archived. No new replies allowed.