Skip to main content
GameDev.net gamedev.net
🔒 Locked

Unformated io

Started by Nanook Jan 21, 2012 at 3:47 PM 4 replies 1k views
Original Post
Nanook
Nanook
I've been using read and write on fstream, but have been told there could be trouble with alignment on different platforms.. so if I write the data on my windows computer using fstream or in a maxscript that exports my model and then want to load it on a different platform I might get into trouble when I load it.. How do you solve this? I guess I could use formated io and the io operators, but this is slower and would take up more disk space..

Writing on platform1

float array[2] = {42.0, 42.0}
filestream.write(reinterpret_cast<char*>(array), sizeof(float) * 2);


Reading on platform2, the data might not be correct due to alignment..


float array[2];
filestream.read(reinterpret_cast<char*>(array), sizeof(float) * 2);
Sik_the_hedgehog
Sik_the_hedgehog
You are referring to the endianess issue, correct? Some processors are big endian, some are little endian. The most common solution is to read each byte individually and then make the number out of that. Pretty easy, really.

Floating point is a more complex issue. I think all processors these days use the same floating point formats (somebody correct me if I'm wrong), but if you want to be safe, you're better off either trying to use only integers or storing those values using your own floating point format (OK, could be the same as IEEE's, but the point is that you need to parse it manually).
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.
Cornstalks
Cornstalks
Serializing your data is a common solution. I have yet to use Google's Protocol Buffers, but I do intend to in my game I'm working on.


Floating point is a more complex issue. I think all processors these days use the same floating point formats (somebody correct me if I'm wrong)

x86 processors all use the same endianness (little-endian) for floating point numbers (and every other data type for that matter). However, there are processors that are different, and if the OP is targeting any computer other than an x86 machine he'll need to account for that. (reference)
Sik_the_hedgehog
Sik_the_hedgehog
[quote name='Sik_the_hedgehog' timestamp='1327162115' post='4904856']
Floating point is a more complex issue. I think all processors these days use the same floating point formats (somebody correct me if I'm wrong)

x86 processors all use the same endianness (little-endian) for floating point numbers (and every other data type for that matter). However, there are processors that are different, and if the OP is targeting any computer other than an x86 machine he'll need to account for that. (reference)[/quote]
Yeah, this is what I'm wondering actually. Of course the formats remain the same within the x86 family, but does this remain true for e.g. ARM, which is becoming more commonplace thanks to mobile devices using it? (also if you work on consoles PowerPC would be relevant as well)
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.
osmanb
osmanb
Endianness is the only thing you need to really worry about. All of the listed platforms (and every other important platform in existence) uses the standard IEEE floating point representation.
Nanook
Nanook
Ah okay I see..

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.