On Sat, 3 Jul 2004 15:52:14 +0100, Fraser Ross
<fraserATmembers.v21.co.unitedkingdom> wrote:
"John Harrison" >
>> What are the contents of C:\File.ext?
> Its unimportant. The file only has to have at least sizeof(unsigned
> short)
> characters.
No that is incorrect, is has to be text which can be converted to an
integer, optionally preceded by whitespace.
You seem to think you are doing a binary read of sizeof(unsigned short)
bytes but that is not the case. You are reading text and that text must
be
a number.
This is how you should do a binary read (minus error checking)
std::fstream I("C:\\File.ext", ios_base::binary);
unsigned short a;
I.read(static_cast<char*>(&a), sizeof a);
Use read for binary reads not >>.
Thanks. Two other questions I've are: Why use ios_base and not ios?
Can a
use of read as above not be suitable for some systems?
Fraser.
ios_base is standard but ios will also work since ios is derived from
ios_base. Some old books will say ios since ios_base is a newer addition
to C++.
ios_base defines all the thing that apply to all types of streams. ios
defines things that only apply to character streams, and there is also
wios which defines things that only apply to wide character streams.
read is suitable for all systems but the results on one system will differ
from results on another system. You cannot take the same file and the same
program and run them on two different systems and expect to get the same
results. Similarly you cannot take a file which has been created using
write on one system and then use read on another system and be sure to get
the same data back. It might work, it might not. Basically binary I/O is
not portable.
john