C
canilao
Hi All,
So I ran into an issue with using the ! operator with fstream:
// Make sure the file is ready to be read before we move on.
1: fstream fileStream;
2: fileStream.open("c:\\some_file.txt");
// Try to open until success.
3: while(!fileStream) fileStream.open("c:\\some_file.txt");
// Close the file since we know its complete written to disk.
4: fileStream.close();
The idea is that I do not want to move on until the file is readable.
What I was seeing was if the call to open() on line 2 failed and the
call to open on line 3 succeeded, the ! operator on line 3 still
always returned false!? On the other hand, if the call to open() on
line 2 succeeded, the ! operator on line 3 return true...
So it seems if open fails the first time, the state checked by the !
operator never changes?
The way I got around it was either using c-style fopen or re-writing
line 3 like this:
3: while(!fileStream.is_open()) fileStream.open("c:\
\some_file.txt");
Is this how fstream works?
Thanks,
Chris Anilao
So I ran into an issue with using the ! operator with fstream:
// Make sure the file is ready to be read before we move on.
1: fstream fileStream;
2: fileStream.open("c:\\some_file.txt");
// Try to open until success.
3: while(!fileStream) fileStream.open("c:\\some_file.txt");
// Close the file since we know its complete written to disk.
4: fileStream.close();
The idea is that I do not want to move on until the file is readable.
What I was seeing was if the call to open() on line 2 failed and the
call to open on line 3 succeeded, the ! operator on line 3 still
always returned false!? On the other hand, if the call to open() on
line 2 succeeded, the ! operator on line 3 return true...
So it seems if open fails the first time, the state checked by the !
operator never changes?
The way I got around it was either using c-style fopen or re-writing
line 3 like this:
3: while(!fileStream.is_open()) fileStream.open("c:\
\some_file.txt");
Is this how fstream works?
Thanks,
Chris Anilao