fstream issue with ! operator

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
 
P

progmanos

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/iostream/fstream/is_open.html ).
 
M

Martin York

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/iostream/ios_base/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;
}
 
E

Eric Pruneau

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...
 
C

canilao

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/iostream/ios_base/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
 
P

progmanos

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/iostream/ios_base/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;



}- Hide quoted text -

- Show quoted text -

What could cause an error state?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fstream Buffers 26
Mutability issue 1
fstream File i/o 1
Issue with textbox script? 0
eof error using '>>' in fstream 2
Issue with key down - JS 3
problem with fstream 3
API delay issue on Godaddy shared hosting 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top