There's some code I wrote that should read all new lines in a text file, but it gets stuck on one of the loops it runs.
The code is as follows:
#define MAX_MESSAGE_LENGTH 200;
fstream("some/random/file.txt", ios::in | ios::out);
streampos fileSizeReference = 0;
vector<string> messages;
vector<string> onDisplay;
char message[MAX_MESSAGE_LENGTH];
if((int)fileSizeReference == 0)
fileReader.seekg(0);
else
fileReader.seekg((int)fileSizeReference + 1);
cout << "Test" << endl;
// Add all new messages to the messages vector
do
{
fileReader.getline(message, MAX_MESSAGE_LENGTH);
string newMsg = message;
messages.push_back(newMsg.substr(0, MAX_MESSAGE_LENGTH));
}
while (!fileReader.eof());
//
cout << "Test" << endl;
fileReader.seekg(0, ios::beg);
// Set the newest messages in the onDisplay vector
for(int i = 0; i < amountOfMessages; i++)
{
onDisplay[i] = messages[messages.size() - (i + 1)];
}
//
cout << "Test" << endl;
// Display new messages
int current_Y = 0;
for(int i = 0; i < amountOfMessages; i++)
{
current_Y = renderText(messages[i], current_Y);
}
//
// set the new file size as the fileSizeReference
fileReader.seekg(0, ios::end);
fileSizeReference = fileReader.tellg();
The text file looks like this:
Hello World!
Carpe Diem
Random Message
Whenever I run this code, I don't get past the first do-while loop. This is the first run, so the fileSizeReference is 0.
The newMsg variable within the loop is always an empty string, just like the message array.
Does any one of you know why my code gets stuck? Thank you in advance!