I have written the following method that essentially reads the newest lines out of a log file that is being written continuously by another thread. This code runs on a different thread and is driven by a timer (fires off every 5 seconds). It's very simple. Here's what it looks like:
void FileManager::ReadFile()
{
std::vector<std::string> vecLines;
std::string line;
m_InputStream.clear();
while (std::getline(m_InputStream, line))
{
vecLines.push_back(line);
}
if (! vecLines.empty())
{
OnFileUpdate(vecLines);
}
}
My question, is this code safe? Basically, the algorithm I perform is that I open the input stream ahead of time and seek to the end of the file. Then when this method gets called, if there are any new lines to be read, then they will be read in here. If there are any new lines, then any interested clients of this class will be notified (via the OnFileUpdate() call).