ifstream doesn't clear the failbit flag when open file, which violate the...
Brought to you by:
complement
Description: ifstream doesn't clear the failbit flag when open file, which violate the C++ standard, and cause confusing behavior of the user application.
Detail:
This bug is find in Android development environment, which have stlport 5.0 in it. And I also reviewed the code in latest released version 5.2.1 which should also have this issue.
REPRODUCE EXAMPLE:
std::ifstream sequenceFile(/*file path not exist*/ "/mnt/sdcard/test1.txt"); if (sequenceFile.is_open()) { sequenceFile.open(/*file path existing*/ "/mnt/sdcard/test2.txt"); } while (!sequenceFile.iseof()) // this will got infinite loop. { std::string line; getLine(sequenceFile, line); }
RELATED SOURCE CODE: (from _fstream.h)
505 void open(const char* __s, ios_base::openmode __mod = ios_base::in) { 506 if (!this->rdbuf()->open(__s, __mod | ios_base::in)) // <---------if failed, doesn't call this->clear() 507 this->setstate(ios_base::failbit); 508 }
OPERATIONAL ENVIRONMENT:
Android developer environment.
COMPILER VERSION:
g++ (GCC) 4.8
STLPORT VERSION
5.0 and 5.2
Looks you mean something like this:
In part of "which violate the C++ standard" message looks not correct too.
The same opinion here: http://www.cplusplus.com/reference/fstream/ifstream/open/
<snip>
The function clears the stream's state flags on success (setting them to goodbit). In case of failure, failbit is set.
</snip>
But is any reference to STANDARD or DRAFT that say the same? I don't see.
Opposite opinion is here: https://groups.google.com/forum/#!topic/comp.lang.c++/ajqpgwCS0UU
(in case of fail use .clear() explicitly).
But nevertheless, at least it is reasonable about clear error state if open was successful...