Jump to content
• Advertisement

Unformated io

This topic is 2824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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); 

Share this post

Share on other sites
Advertisement
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).

Share this post

Share on other sites
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)

Share this post

Share on other sites
[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)

Share this post

Share on other sites
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.

Ah okay I see..

Share this post

Share on other sites

• Advertisement
• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• Advertisement

• Popular Now

• 16
• 11
• 23
• 42
• 75
• Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!