By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 827 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

fstream::read(..);

P: n/a
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 ?
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"dumboo" <vt***@yahoo.com> wrote in message
news:2g************@uni-berlin.de...
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
Jul 22 '05 #2

P: n/a
hi there
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2g************@uni-berlin.de...

"dumboo" <vt***@yahoo.com> wrote in message
news:2g************@uni-berlin.de...
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 :-)
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.