Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

endian independent file io in C++

Name: Anonymous 2007-02-24 21:45 ID:XOqzVx8R

What is the correct way to do this?  I'm doing:

data->put (x % 256);
data->put (x / 256);

and

x = data->get ();
x += data->get () * 256;

But I'm sure there's a better way.  Also, this way doesn't work with negative numbers.

Name: Anonymous 2007-02-24 21:47 ID:ioAywf6I

If disk space isn't an issue, store the value as a string.

Name: Anonymous 2007-02-24 21:59 ID:N6KVUNp/

hton() function

Name: Anonymous 2007-02-24 22:32 ID:3QL13q/R

>>1
One word, the forced endianness of file io. Thread over.

Name: Anonymous 2007-02-24 23:11 ID:W7X2Ljhb

>>3 is an EXPERT PROGRAMMER.

Use the API of your choice to automatically translate the native endian-ness of the machine that the code is running on to the machine-independent network byte order.  If all persisted data is stored in network byte order, the endian nature of the machine reading or writing the file becomes irrelevant.

(This is a classic technique taught in any self-respecting OpSys course.)

Name: Anonymous 2007-02-24 23:17 ID:3QL13q/R

>>5
you are like that guy who explains the punchlines of jokes after someone else tells one.

Name: Anonymous 2007-02-25 2:41 ID:sKz2cLo1

>>6
you are the punchline of one of god's jokes.

Name: Anonymous 2007-02-25 5:24 ID:Heaven

>>6

Name: Anonymous 2007-02-25 16:46 ID:oZf5IOXT

>>8

Name: Anonymous 2007-02-25 21:25 ID:xqvAnfu1

>>6

Name: Anonymous 2007-02-26 1:30 ID:DwjDL1Ru

Name: Anonymous 2007-02-26 3:40 ID:kYBWtwzp

Name: Sgt.Kabu樯뛒kiman�෯ 2012-05-28 22:50

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List