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