Sign in to follow this  

saving and restoring data issues

This topic is 4106 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

Hello, A few questions about saving and restoring data in a cross platform application: Say I have a written a cross platform application - say PC and Mac - and I want to be able to save some status of my program (not necessarily a game) on the PC and then load the same information back into the program on a Mac (or maybe another PC). Question 1: What about integer size in bytes? For example, what if on the PC my integer size is 4 bytes, and on the Mac its 2 bytes. How do I handle that? How do I guarantee byte size over different platforms? Question 2: Is endianess a problem with floating point types? Question 3: Is there a technique of saving my data in some sort of custom file format that makes resolving these issues easier? I've been searching the internet, but I have not really found anything helpful, so I'm not sure if these questions are silly or not and I'm just misunderstanding the whole problem :) If someone could enlighten me that would be great. metrix

Share this post


Link to post
Share on other sites
It's all in the writing (encoding) and reading (decoding) of the data to be transfered.

1. to take into account type/data sizes, have a standard size in the data format of your file. For example, for ints, reserve 4 bytes of space in the data (be sure that every target supports the values you put in there).

2. yes.

3. you can, for example, read and write per byte. This way you force your file format to be one type of endianess.
You can also do a check if the current system is big or little endian and read/write the data accordingly.


Hope it enlightens you a bit,
greetings.

Share this post


Link to post
Share on other sites
Quote:
Question 1: What about integer size in bytes? For example, what
if on the PC my integer size is 4 bytes, and on the Mac its 2 bytes.
How do I handle that? How do I guarantee byte size over different platforms?

First, you're probably pretty safe assuming that a byte is 8 bits. Most hardware designers have now reached a consensus on that one :)

As for the size of integers, when you care exactly about how many bytes your types are using, don't use char/short/int/long, use types that explicitly tell you their size. On Linux (for example) stdint.h provides types such as int8_t, uint8_t, int16_t, etc... You can easily define your own similar types.

Quote:
Question 3: Is there a technique of saving my data in some sort of
custom file format that makes resolving these issues easier?

I'm going to avoid this question and answer a slightly different one... :)

The main problem is if you dump/load structs directly to/from disk, since you're assuming that the memory layout of structs will be the same on every platform. Of course, size, endianness and packing differences ruin that.

The main thing you need to do is to have something in your program checking and converting data read from the file to the equivalent data in memory. If you use a binary file format, you might specify that all integers be stored on disk in 32-bit big-endian format. Your loading code can act knowing that it is reading a 32-bit big-endian integer, and then convert appropriately for the platform on which the program is running (e.g. convert to little-endian).

Another way would be to use a text-based format. All integers then have to be parsed (probably by the std lib) and so converting to the correct format for your platform is than effectively automatic. (Of course you would still have to worry about wrapping on platforms with 16-bit integers for example).

Hope that rambling helped somewhat.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
IF you only need to store plain data, and there's no need for particular effiency, the answer to questions 1, 2 & 3 might be: "simply use XML!"

There are various freely available wrapper solutions available that allow you to easily use XML for data serialization.

Share this post


Link to post
Share on other sites
T_Janssen & Neex:
Thanks for the help, I have a good idea now of how I want to do this.

Anonymous:
Thanks for the suggestion, I looked into using XML, however for my
purposes I think the text based solution will be best.

Share this post


Link to post
Share on other sites

This topic is 4106 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this