C++ writing unsigned int to a file

k++

k++

Associate
Joined
5 Oct 2004
Posts
585
Location
London
I have looked everywhere and can't for the life of me find exactly what I'm looking for.

I'm trying to write a load of unsigned integers to a binary file, the problem is I can only find how to write characters. That isn't a binary file really, I would like to store just 32-bits for each uint.

ofstream.write() requires a character array, if I typecast my uint to (char*) it reverses the order so the number (in hex) 04004000 is written as 00400040.

How nuts is that.

I'm also having a problem reading these files properly, I have written a Java version of my program which works perfectly except it doesn't support unsigned integers so I'm stuck with 64-bit numbers. I can't seem to load the numbers one at a time using c++

:confused:
 
I have just discovered reinterpret_cast and think I have solved my problem.

Thanks for looking though :)
 
Im pretty sure ostream.write actually takes a const char* with a size of how much to write. You should not use reinterpret_cast to do what your trying to do either. I remember doing something like this ages ago and seem to recall using the ostream.put to write individual characters. Long time since ive done any not .NET c++ :)
 
ofstream.write() requires a character array, if I typecast my uint to (char*) it reverses the order so the number (in hex) 04004000 is written as 00400040.

How nuts is that.

That'll be because the number is stored in memory with little-endian byte order.
 
Back
Top Bottom