Hi.
I want to read through a file and search for variables saved in it.
However if the stream reads the last char I got errors like EOF.
For example the following code would not work as intended to :
bool BaseFileReader::Valid() const
{
if (!File.is_open())
std::cout << "\n\nERROR: File is not open!";
if (File.fail())
std::cout << "\nERROR: Either failbit or badbit is set!";
if (File.eof())
std::cout << "\nERROR: EOF is set!";
return (File.is_open() && !File.fail() && !File.eof() && File.good());
}
bool BaseFileReader::LoadFromFile(const char* filepath)
{
if (!filepath)
{
SetERR(ERR_UNKNOWN); // pointer is invalid
return false;
}
File.open(filepath);
if (!Valid())
{
SetERR(ERR_OPENFILE); // Can't open file
return false;
}
// Here goes my reading algorithm, but this loop expresses better what the problem is
for (int i = 0;i < 2;i ++) // Should be printing the whole content two times
{
std::cout << "\n" << i+1 << "x print : \n==========\n";
// Last Valid() call would print EOF and fail()
while(Valid())
std::cout << (char)File.get();
std::cout << "\n==========";
// After EOF I'm not able to set the get-pointer
File.seekg(std::ios_base::beg);
}
SetERR(ERR_GOODBIT);
Clear();
return true;
}
The big deal is, that when my Reader reads keywords at the end of a file and then wants to read a keyword before that, it can't.
Since reading the last characters, which represent the variable, would lead to reading the last character of the file.
And that means EOF, so the Reader is not able to read any data, unless I reopen the file.
I already know two ways of solving that.
I could just add a 'non-number' char at the end, so it wouldn't be read by the Reader.
But I find this approach more uglier than solving it by a different code.
My question is if there is a way to "safe" the stream after getting errors like EOF, without reopening it.
Thanks in advance.