In my program, I've redirected stdout to print to a file 'console.txt'. A function writes to that file like this:
void printToConsole(const std::string& text, const TCODColor& fc, const TCODColor& bc)
{
// write the string
cout << text << "@";
// write the two color values
cout << static_cast<int>(fc.r) << " "
<< static_cast<int>(fc.g) << " "
<< static_cast<int>(fc.b) << " "
<< static_cast<int>(bc.r) << " "
<< static_cast<int>(bc.g) << " "
<< static_cast<int>(bc.b) << " " << endl;
}
I have a function that reads from that file that looks like this:
void Console::readLogFile()
{
ifstream log("console.txt", ifstream::in);
if(!log.is_open())
{
cerr << "ERROR: console.txt not found!" << endl;
return;
}
// read new input into the stack
char str[256];
while(!log.eof())
{
log.getline(str, 256);
cerr << "str: " << str << endl;
stk.push(static_cast<string>(str));
// stk is a std::stack<std::string> member of the class this function
// belongs to.
}
cerr << endl;
/* Do some stuff with str and stk here */
log.close();
clearLogFile();
}
void Console::clearLogFile()
{
FILE* log;
log = fopen("console.txt", "w");
fclose(log);
}
Often, console.txt is empty when readLogFile
is called. I would expect that the while(!log.eof())
loop would never execute in that case, but it does. There is always at least one extraneous blank line in the file, sometimes two, and when input is read from the file, the input line is sandwiched between two blank lines. After a few calls to this function, the while(!log.eof())
loop then goes into an infinite loop pulling blank lines from the file. A typical runthrough of the program looks like this:
str:
str: Player moved.@191 191 191 0 0 0
str:
str:
str: Player moved.@191 191 191 0 0 0
str:
str: // there should be a 'Player moved.' line in here
str:
str: // here as well
str:
str: // also here
str:
str:
str: Player moved.@191 191 191 0 0 0
str:
str:
str:
str:
str:
str:
str:
(onto infinite loop)
Can anyone see what I'm doing wrong here?
EDIT: As Amardeep suggested, I changed the while(!log.eof())
loop to a do{...}while(!log.fail);
loop. This fixed the infinite loop problem, but not the extraneous lines. The program behaves as before, except where it once went into the infinite loop, it now reads nothing but blank lines where it should read input, like this:
str:
str:
str:
str:
(etc.)