473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

fstream issue with ! operator

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

Jun 27 '08 #1
6 1951
On Jun 17, 5:08*pm, cani...@gmail.com wrote:
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
I think so. I'm not sure what applying the not operator to the
fstream object itself would do. However, you are correct in modifying
the code to use the is_open object since it returns a boolean value
depending on whether the object "is associated with a file"(
http://www.cplusplus.com/reference/i...m/is_open.html ).
Jun 27 '08 #2
On Jun 17, 3:10*pm, progmanos <tatum.ras...@gmail.comwrote:
>
So I ran into an issue with using the ! operator with fstream:
I'm not sure what applying the not operator to the
fstream object itself would do. However, you are correct in modifying
the code to use the is_open object since it returns a boolean value
Well actually no is_open() is not correct.
If used in a boolean context an fstream object will convert itself to
bool by testing its internal flags.
So if '!fileStream' is returning false then you will not be able to
read from the file even if it opened correctly. This is because some
other flag is indicating an error state (and this flag will be checked
when other methods are invoked on the object).

You should reset the error flags before attepting to re-open the file.

See: http://www.cplusplus.com/reference/i...e/iostate.html
For what else can go wrong.

#include <fstream>
#include <iostream>

int main()
{
std::fstream data;
data.open("./plop");

while(!data)
{
std::cout << "Failed" << std::endl;
// Pause here
std::cout << "Retrying" << std::endl;

// Put the state of the object back into its original state.
data.clear();

// Retry opening the file.
data.open("./plop");
if (data.is_open())
{
std::cout << "Open" << std::endl;
if (!data)
{
std::cout << "Reading from this file will fail." <<
std::endl;
}
}
}

std::cout << "Finished" << std::endl;
}

Jun 27 '08 #3

<ca*****@gmail.coma écrit dans le message de news:
f4**********************************...oglegroups.com...
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
Consider your trying to open a read-only file.

while(!fileStream.is_open())
fileStream.open("c:\\some_file.txt");

is an infinite loop...


Jun 27 '08 #4
On Jun 17, 5:55 pm, Martin York <Martin.YorkAma...@gmail.comwrote:
On Jun 17, 3:10 pm, progmanos <tatum.ras...@gmail.comwrote:
So I ran into an issue with using the ! operator with fstream:
I'm not sure what applying the not operator to the
fstream object itself would do. However, you are correct in modifying
the code to use the is_open object since it returns a boolean value

Well actually no is_open() is not correct.
If used in a boolean context an fstream object will convert itself to
bool by testing its internal flags.

So if '!fileStream' is returning false then you will not be able to
read from the file even if it opened correctly. This is because some
other flag is indicating an error state (and this flag will be checked
when other methods are invoked on the object).

You should reset the error flags before attepting to re-open the file.

See:http://www.cplusplus.com/reference/i...e/iostate.html
For what else can go wrong.

#include <fstream>
#include <iostream>

int main()
{
std::fstream data;
data.open("./plop");

while(!data)
{
std::cout << "Failed" << std::endl;
// Pause here
std::cout << "Retrying" << std::endl;

// Put the state of the object back into its original state.
data.clear();

// Retry opening the file.
data.open("./plop");
if (data.is_open())
{
std::cout << "Open" << std::endl;
if (!data)
{
std::cout << "Reading from this file will fail." <<
std::endl;
}
}
}

std::cout << "Finished" << std::endl;

}
Thanks!!! This helps out alot!

- Chris
Jun 27 '08 #5
On Jun 17, 6:55*pm, Martin York <Martin.YorkAma...@gmail.comwrote:
On Jun 17, 3:10*pm,progmanos<tatum.ras...@gmail.comwrote:
So I ran into an issue with using the ! operator with fstream:
I'm not sure what applying the not operator to the
fstream object itself would do. However, you are correct in modifying
the code to use the is_open object since it returns a boolean value

Well actually no is_open() is not correct.
If used in a boolean context an fstream object will convert itself to
bool by testing its internal flags.

So if '!fileStream' is returning false then you will not be able to
read from the file even if it opened correctly. This is because some
other flag is indicating an error state (and this flag will be checked
when other methods are invoked on the object).

You should reset the error flags before attepting to re-open the file.

See:http://www.cplusplus.com/reference/i...e/iostate.html
For what else can go wrong.

#include <fstream>
#include <iostream>

int main()
{
* * std::fstream * *data;
* * data.open("./plop");

* * while(!data)
* * {
* * * * std::cout << "Failed" << std::endl;
* * * * // Pause here
* * * * std::cout << "Retrying" << std::endl;

* * * * // Put the state of the object back into its original state.
* * * * data.clear();

* * * * // Retry opening the file.
* * * * data.open("./plop");
* * * * if (data.is_open())
* * * * {
* * * * * * std::cout << "Open" << std::endl;
* * * * * * if (!data)
* * * * * * {
* * * * * * * * std::cout << "Reading from this file willfail." <<
std::endl;
* * * * * * }
* * * * }
* * }

* * std::cout << "Finished" << std::endl;

}- Hide quoted text -

- Show quoted text -
What could cause an error state?
Jun 27 '08 #6
On Jun 20, 11:42*am, progmanos <tatum.ras...@gmail.comwrote:
On Jun 17, 6:55*pm, Martin York <Martin.YorkAma...@gmail.comwrote:
You should reset the error flags before attepting to re-open the file.
See:http://www.cplusplus.com/reference/i...e/iostate.html
For what else can go wrong.
What could cause an error state?
See:http://www.cplusplus.com/reference/i...e/iostate.html
For what else can go wrong.
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Frédéric Manzanares | last post by:
hello, my problem: I want to habe one Class with write and read in a file. i have overloaded the operator >> and <<. class c_File { public : fstream fs;
6
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
4
by: Mathieu Malaterre | last post by:
Hello, I am trying to take advantage of the formatted ouput operator of ostream to rewrite this piece of code /more nicely/: oftream fp("/tmp/foo", std::ios::out | std::ios::binary); uint32_t...
7
by: John Ratliff | last post by:
Is this a legitimate (standards compliant) way to check for errors in file I/O. #include <fstream> fstream output("file.ext", std::ios_base::out, std::ios_base::binary); if (output) { //...
2
by: Javi | last post by:
Hi!. I'm trying to do something like this but results in lot of errors. Can anybody tell me how to do it (first of all I hope you can grasp my idea)?: myfile.cpp #include <iostream> #include...
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
2
by: jjcp | last post by:
I would like to say thanks in advance for insight anyone can shed on this for me. Long story short from time to time I need to us C++ to take a list of file and make an index out of them in...
5
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
6
by: Gaijinco | last post by:
Should this do something? #include <fstream> #include <string> int main() { std::fstream filestr ("test.txt", std::fstream::in | std::fstream::out); std::string s="";
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.