fstream::read(..);

Discussion in 'C++' started by dumboo, May 18, 2004.

  1. dumboo

    dumboo Guest

    hi there
    i m using fstream object for reading through and writing to my file, using
    following code

    char c;
    while(!m_aa.eof())
    {
    m_aa.read(&c, sizeof(c));
    m_oo.write(&c, sizeof(c));

    cout<<"\nout to c:"<<(int)c;
    }

    when i saw the outoput of 'cout' and checked the output in HEX editor,
    it always showed me 0xCC (-52) in first byte(which is not present in input
    file), i dont know why is it occouring, i had open the files properly in
    binary mode, and m not doing any kind of preprocessing, except the above
    operation,
    I m using MSVC 6.0 any suggestion ?
    dumboo, May 18, 2004
    #1
    1. Advertising

  2. "dumboo" <> wrote in message
    news:...
    > hi there
    > i m using fstream object for reading through and writing to my file, using
    > following code
    >
    > char c;
    > while(!m_aa.eof())
    > {
    > m_aa.read(&c, sizeof(c));
    > m_oo.write(&c, sizeof(c));
    >
    > cout<<"\nout to c:"<<(int)c;
    > }
    >
    > when i saw the outoput of 'cout' and checked the output in HEX editor,
    > it always showed me 0xCC (-52) in first byte(which is not present in input
    > file),


    In the first byte? I could understand it in the last byte, because your
    while loop is wrong.

    I don't know why so many people get this wrong, but eof is not true when you
    are at the end of a file. eof is used to test after a read has be done
    whether that read failed because you *were* at the end of a file. So in your
    code the following happens

    1) read upto end of file
    2) at end of file, eof returns false
    3) call read, fails, because you were at the end of file
    4) output garbage value of c
    5) now eof is true, quit loop.

    The right way to write this sort of loop is

    while (m_aa.read(&c, sizeof(c))
    {
    m_oo.write(&c, sizeof(c));
    cout<<"\nout to c:"<<(int)c;
    }
    if (m_aa.eof())
    {
    // ok we hit end of file
    }
    else
    {
    // some other sort of error
    }

    > i dont know why is it occouring, i had open the files properly in
    > binary mode, and m not doing any kind of preprocessing, except the above
    > operation,
    > I m using MSVC 6.0 any suggestion ?
    >


    I'm not sure, the problem I described above doesn't exactly match what you
    are describing. I would suggest posting a complete compilable program that
    demonstrates this behaviour. Possibly the error is in the code you haven't
    posted so far. In any case make the improvement I suggested above.

    john
    John Harrison, May 18, 2004
    #2
    1. Advertising

  3. dumboo

    dumboo Guest

    hi there
    "John Harrison" <> wrote in message
    news:...
    >
    > "dumboo" <> wrote in message
    > news:...
    > > hi there

    [...]
    > while (m_aa.read(&c, sizeof(c))
    > {
    > m_oo.write(&c, sizeof(c));
    > cout<<"\nout to c:"<<(int)c;
    > }
    > if (m_aa.eof())
    > {
    > // ok we hit end of file
    > }
    > else
    > {
    > // some other sort of error
    > }

    [...]
    Thanks John that solved the problem :)
    dumboo, May 18, 2004
    #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:
    739
    Armando
    Jan 29, 2004
  2. Christian Henke

    fstream messes up order at read out

    Christian Henke, Mar 6, 2004, in forum: C++
    Replies:
    7
    Views:
    429
    Christian Henke
    Mar 7, 2004
  3. Don Kim
    Replies:
    13
    Views:
    787
    Francis Glassborow
    Jan 24, 2005
  4. Replies:
    2
    Views:
    1,138
    Abecedarian
    May 12, 2005
  5. Sachin Garg
    Replies:
    1
    Views:
    478
    Ron AF Greve
    Aug 22, 2008
Loading...

Share This Page