fstream issue with ! operator

Discussion in 'C++' started by canilao@gmail.com, Jun 17, 2008.

  1. Guest

    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 17, 2008
    #1
    1. Advertising

  2. progmanos Guest

    On Jun 17, 5:08 pm, 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/iostream/fstream/is_open.html ).
     
    progmanos, Jun 17, 2008
    #2
    1. Advertising

  3. Martin York Guest

    On Jun 17, 3:10 pm, progmanos <> wrote:

    >
    > > 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/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;
    }
     
    Martin York, Jun 17, 2008
    #3
  4. Eric Pruneau Guest

    <> a écrit dans le message de news:
    ...
    > 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...
     
    Eric Pruneau, Jun 18, 2008
    #4
  5. Guest

    On Jun 17, 5:55 pm, Martin York <> wrote:
    > On Jun 17, 3:10 pm, progmanos <> wrote:
    >
    >
    >
    > > > 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/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
     
    , Jun 18, 2008
    #5
  6. progmanos Guest

    On Jun 17, 6:55 pm, Martin York <> wrote:
    > On Jun 17, 3:10 pm,progmanos<> wrote:
    >
    >
    >
    > > > 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/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?
     
    progmanos, Jun 20, 2008
    #6
  7. Martin York Guest

    Martin York, Jun 21, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Armando
    Replies:
    6
    Views:
    763
    Armando
    Jan 29, 2004
  2. David Briggs

    Problem with fstream and operator>>

    David Briggs, May 23, 2004, in forum: C++
    Replies:
    6
    Views:
    3,779
    John Harrison
    May 26, 2004
  3. jjcp
    Replies:
    2
    Views:
    406
  4. Replies:
    5
    Views:
    538
    James Kanze
    Apr 2, 2007
  5. Replies:
    8
    Views:
    399
Loading...

Share This Page