PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Binary data

In my program I tried to use this code to store and get unsigned short values in binary file, but it doesn't work. I think there is much more simple way to do it, but I don't know it (e.g. How to store floats)? Maybe there is binary format in fprintf?

void FileGetBShort(FILE * inFile, unsigned short * inShort)
{
unsigned char Char1;
unsigned char Char2;
Char1 = fgetc(inFile);
Char2 = fgetc(inFile);
*inShort = 0;
*inShort = (*inShort | Char1);
*inShort = (*inShort << 8);
*inShort = (*inShort | Char2);
}

void FilePutBShort(FILE * inFile, unsigned short * inShort)
{
unsigned char Char1;
unsigned char Char2;
Char1 = (* inShort >> 8);
Char2 = (*inShort & 0x00FF);
fputc(Char1, inFile);
fputc(Char2, inFile);
}
This thread belongs in General Developer Discussion as it has nothing to do with PocketHAL or PocketFrog.

void FileGetBShort(FILE * inFile, unsigned short * inShort)
{
fread(inShort, sizeof(unsigned short), 1, inFile);
}

void FilePutBShort(FILE * inFile, unsigned short * inShort)
{
fwrite(inShort, sizeof(unsigned short), 1, inFile);
}

You're much better off reading entire structs of data, or arrays of shorts, than one at a time for performance reasons.

Dan East
Thanks Dan, It was so simple...
Dan East wrote:
You're much better off reading entire structs of data, or arrays of shorts, than one at a time for performance reasons.


Consider how much data you have though. Unless it's big, I wouldn't worry too much about performance. It takes a decent amount of data to really be slow.

This is where it will pay to do a bit of testing beforehand (on memory and on storage card) to determine approximately how long your routines will take given your expected data size.

Don't know your expected data size? That may be an indication that you aren't finished your design phase.
Dave Johnston · 05 Jan 2004, 17:16 UTC
Dan East wrote:
void FileGetBShort(FILE * inFile, unsigned short * inShort)
{
fread(inShort, sizeof(unsigned short), 1, inFile);
}

void FilePutBShort(FILE * inFile, unsigned short * inShort)
{
fwrite(inShort, sizeof(unsigned short), 1, inFile);
}


(I think) This can lead to problems if this data is written on a big-endian machine, and read on a little-endian machine, or vice versa... but fortunately, all CPU's used in PocketPC's are the same endian.

Just something to be aware of (maybe :))
BIGBEN · 05 Jan 2004, 17:45 UTC — Up to 100 kb
My game will use chars shorts and floats to store level data. Levels are now around 10 kb but could be up to around 100 kb. Would this metod also work for floats?
mlepage wrote:
Consider how much data you have though. Unless it's big, I wouldn't worry too much about performance. It takes a decent amount of data to really be slow.


Sorry, I just don't think that way. Any optimization that blatant should always be utilized. That's simply good coding practice, which is something that shouldn't be turned on and off just because you aren't coding a critical inner loop in a game.

Dan East
Dave Johnston wrote:
(I think) This can lead to problems if this data is written on a big-endian machine, and read on a little-endian machine, or vice versa... but fortunately, all CPU's used in PocketPC's are the same endian.

Just something to be aware of (maybe :))


That's correct. If he were to target some other processor with a different endianess than ARM he would have to convert the data at load time. The Quake source code contains a routine to test endianess, as well as a technique to use to convert at load time. It's really simple stuff.

Dan East
BIGBEN · 07 Jan 2004, 06:07 UTC — If I understand you right...
To achieve more portability I need to do this? :

typedef unsigned __int16 ushort;
...
void FileGetBShort(FILE * inFile, ushort * inShort) {...}
void FilePutBShort(FILE * inFile, ushort * inShort) {...}

Is it enough?
Or you talking about something else?
Dan East":3q2d10tt]

I agree that you should always practice good coding.

But we could debate which is the better code. Reading in structs will lend to better performance, but will be less portable (or at least less easily portable). Reading in arrays can be less flexible than reading in data individually (if your file format varies).

I would not read in structs. I would read in individual values where individual values are in the file, and arrays where multiple values are in the file. To me, that strikes the right balance of flexibility and performance.

There's also a bit of an 80/20 phenomenon happening here. Most of your data will be arrays, so reading them as arrays will solve most performance problems. The remaining data may be individual values, but there will be relatively few of them. To try to read them as arrays (say they are all int) might work, but the cost of flexibility doesn't gain you much because they are so few.

I didn't mean to suggest that even arrays of data in the file should be read in as single values one at a time, sorry if I gave that impression.