eof error using '>>' in fstream

Discussion in 'C++' started by David, Feb 9, 2010.

  1. David

    David Guest

    hello,
    I'm trying to figure out a weird problem with opening a file for both
    input and output using fstream...
    compiling using g++ 4.3.2 on debian

    -----
    fstream f1;
    f1.open("./test.txt", std::ifstream::in | std::ifstream::eek:ut);

    f1 << "1 2 3\n" << flush; //dump something into file
    f1.seekg(0); //set get ptr to 0

    int i;
    f1 >> i; //read out '1'

    //cout << " get pos = " << f1.tellg() << " put pos = " << f1.tellp()
    << endl;
    f1 << "a" << flush;

    if (!f1.good())
    {
    cout << "fstream is bad" << endl;
    }
    else
    {
    cout << "fstream is good" << endl;
    }

    ----
    If i run the code above as is, I get eof error when trying to write
    'a' into stream... text.txt reads "1 2 3"
    if uncomment the cout, program completes and text.txt reads "1a2 3"

    according to the '98 c++ open standard there seems to be some
    inconsistencies on how seekg and seekp operates, but i can't find
    anything that explains this behavior...
    http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#136

    Any insights on why this is happening and how i can get this to work
    without the cout is greatly appreciated!


    Thanks!

    ~dAviD
     
    David, Feb 9, 2010
    #1
    1. Advertising

  2. David

    David Guest

    On Feb 8, 10:24 pm, Paavo Helde <> wrote:
    > David <> wrote in news:2341a9db-c380-4f33-ad65-
    > :
    >
    >
    >
    >
    >
    > > hello,
    > > I'm trying to figure out a weird problem with opening a file for both
    > > input and output using fstream...
    > > compiling using g++ 4.3.2 on debian

    >
    > > -----
    > >      fstream f1;
    > >      f1.open("./test.txt", std::ifstream::in | std::ifstream::eek:ut);

    >
    > >      f1 << "1 2 3\n" << flush; //dump something into file
    > >      f1.seekg(0); //set get ptr to 0

    >
    > >      int i;
    > >      f1 >> i; //read out '1'

    >
    > >      //cout << " get pos = " << f1.tellg() << " put pos = " << f1.tellp

    > ()
    > > << endl;
    > >      f1 << "a" << flush;

    >
    > >      if (!f1.good())
    > >      {
    > >           cout << "fstream is bad" << endl;
    > >      }
    > >      else
    > >      {
    > >           cout << "fstream is good" << endl;
    > >      }

    >
    > > ----
    > > If i run the code above as is, I get eof error when trying to write
    > > 'a' into stream... text.txt reads "1 2 3"
    > > if uncomment the cout, program completes and text.txt reads "1a2 3"

    >
    > > according to the '98 c++ open standard there seems to be some
    > > inconsistencies on how seekg and seekp operates, but i can't find
    > > anything that explains this behavior...
    > >http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#136

    >
    > > Any insights on why this is happening and how i can get this to work
    > > without the cout is greatly appreciated!

    >
    > The underlying system may require that you perform seek or some related
    > operation when switching between the reading and writing. This has little
    > to do with C++. For example, from the MSVC fopen() documentation:
    >
    > "However, when you switch between reading and writing, there must be an
    > intervening fflush, fsetpos, fseek, or rewind operation."
    >
    > Both tellg() and tellp() are documented as calling a seek (pubseekoff()),
    > so probably you just need to call one of them when switching from reading
    > to writing.
    >
    > hth
    > Paavo


    I see. I guess it didn't occur to me to look in fopen documentation
    for clues.

    from fopen manpage:
    "Therefore it is good practice (and indeed sometimes necessary under
    Linux) to put an fseek(3) or fgetpos(3) operation between write and
    read operations on such a stream."

    doesn't look like fflush call would work for linux, as it would for
    MSVC.

    Anyways, thanks much for your help!
     
    David, Feb 9, 2010
    #2
    1. Advertising

  3. David

    Kumar Anurag Guest

    On Feb 9, 11:24 am, Paavo Helde <> wrote:
    > David <> wrote in news:2341a9db-c380-4f33-ad65-
    > :
    >
    >
    >
    >
    >
    > > hello,
    > > I'm trying to figure out a weird problem with opening a file for both
    > > input and output using fstream...
    > > compiling using g++ 4.3.2 on debian

    >
    > > -----
    > >      fstream f1;
    > >      f1.open("./test.txt", std::ifstream::in | std::ifstream::eek:ut);

    >
    > >      f1 << "1 2 3\n" << flush; //dump something into file
    > >      f1.seekg(0); //set get ptr to 0

    >
    > >      int i;
    > >      f1 >> i; //read out '1'

    >
    > >      //cout << " get pos = " << f1.tellg() << " put pos = " << f1.tellp

    > ()
    > > << endl;
    > >      f1 << "a" << flush;

    >
    > >      if (!f1.good())
    > >      {
    > >           cout << "fstream is bad" << endl;
    > >      }
    > >      else
    > >      {
    > >           cout << "fstream is good" << endl;
    > >      }

    >
    > > ----
    > > If i run the code above as is, I get eof error when trying to write
    > > 'a' into stream... text.txt reads "1 2 3"
    > > if uncomment the cout, program completes and text.txt reads "1a2 3"

    >
    > > according to the '98 c++ open standard there seems to be some
    > > inconsistencies on how seekg and seekp operates, but i can't find
    > > anything that explains this behavior...
    > >http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#136

    >
    > > Any insights on why this is happening and how i can get this to work
    > > without the cout is greatly appreciated!

    >
    > The underlying system may require that you perform seek or some related
    > operation when switching between the reading and writing. This has little
    > to do with C++. For example, from the MSVC fopen() documentation:
    >
    > "However, when you switch between reading and writing, there must be an
    > intervening fflush, fsetpos, fseek, or rewind operation."
    >
    > Both tellg() and tellp() are documented as calling a seek (pubseekoff()),
    > so probably you just need to call one of them when switching from reading
    > to writing.
    >
    > hth
    > Paavo


    ??
     
    Kumar Anurag, Feb 11, 2010
    #3
    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:
    770
    Armando
    Jan 29, 2004
  2. kartik
    Replies:
    1
    Views:
    558
    Jack Klein
    Oct 30, 2004
  3. Kobu
    Replies:
    10
    Views:
    657
    Keith Thompson
    Mar 4, 2005
  4. Greg
    Replies:
    7
    Views:
    582
    Default User
    Dec 19, 2005
  5. SpreadTooThin

    ifstream eof not reporting eof?

    SpreadTooThin, Jun 13, 2007, in forum: C++
    Replies:
    10
    Views:
    712
    James Kanze
    Jun 15, 2007
Loading...

Share This Page