This site is no longer active and is available for archival purposes only. Registration and login is disabled.

Writing large values to a file


Writing large values to a file

Postby fzammetti » Feb 23, 2004 @ 8:00pm

Ok folks, my brain must be completely asleep today because this one is stumping me big-time...

I have a function that writes out 83 bytes of information for my game. All of the data, except for six fields, are unsigned char's (simple flags and options, a couple of coordinates that are always 0-255, etc). I have a buffer defined as...

unsigned char* outBuffer = (unsigned char*)malloc(83);

...that I put all the data into, then write it out. It works perfectly well for just about all of the data I'm working with, no problem.

However, the six fields I alluded to are DWORD variables, so of course that's the equivalent of 4 unsigned char's.

Here's my question... how do I write them out to my file (really, how do I insert them into my buffer) without losing precision? The code I'm using for the other fields is basically:

int indexer = 0;
*(outBuffer + indexer++) = (unsigned char)volumeLevel;
*(outBuffer + indexer++) = (unsigned char)currentGameLevel;
// ... and so on...

...for the most part the casts aren't necassery, but since some of the variables are actualy ints (that are always in the range of an unsigned char anyway), I do it all the time just to be safe. I of course can't just casts a DWORD to an unsigned char because I'd lose information.

So, I guess another way of asking this is how can I break a DWORD into the four bytes that make it up, write them out, and then re-constitute the four bytes into a DWORD when I read the file back in?

TIA!
...and so I said to Mr. Gates: "$640 billion should be enough for anyone!"
User avatar
fzammetti
pm Insider
 
Posts: 1496
Joined: Jun 4, 2002 @ 6:21pm
Location: Omnytex Technologies


Postby angedelamort » Feb 23, 2004 @ 9:21pm

Last edited by angedelamort on Feb 23, 2004 @ 9:53pm, edited 1 time in total.
User avatar
angedelamort
pm Member
 
Posts: 304
Joined: Mar 7, 2002 @ 11:16pm
Location: Montreal, Quebec


Postby fzammetti » Feb 23, 2004 @ 9:45pm

...and so I said to Mr. Gates: "$640 billion should be enough for anyone!"
User avatar
fzammetti
pm Insider
 
Posts: 1496
Joined: Jun 4, 2002 @ 6:21pm
Location: Omnytex Technologies


Postby jongjungbu » Feb 23, 2004 @ 9:55pm

User avatar
jongjungbu
Not JongJongBu
 
Posts: 3112
Joined: Jun 19, 2001 @ 4:22am
Location: USA


Postby fzammetti » Feb 23, 2004 @ 9:59pm

...and so I said to Mr. Gates: "$640 billion should be enough for anyone!"
User avatar
fzammetti
pm Insider
 
Posts: 1496
Joined: Jun 4, 2002 @ 6:21pm
Location: Omnytex Technologies


Postby angedelamort » Feb 23, 2004 @ 10:06pm

User avatar
angedelamort
pm Member
 
Posts: 304
Joined: Mar 7, 2002 @ 11:16pm
Location: Montreal, Quebec


Postby jongjungbu » Feb 23, 2004 @ 11:51pm

You are right about the different endianess problem and the varying sizes of data types on different platforms. I really haven't had much of a problem with this for PPC-PC programs. It has come up before though, in which case I ended up using #ifdef's for certain reads for the alternate data size. And of course you have to take care of TCHAR factors since sometimes the PC can treat a TCHAR as a char or as an unsigned short.

But in general, particularly the various integer types, I haven't had any problems for portability from PPC to PC and back. Oh and I generally use _tfopen as opposed to fopen. But aside from that, I don't see any functions in what I posted that I haven't used in both evC and visual studio.

Another thing I do is avoid using 'int' explicitly as a data type since that has been documented as varying in size. At least for variables that I intend to fwrite. Thusly I often use long or short etc, or have typedefs for 8-,16-,and 32-bit integers so that I can get some form of consistency in size.

JJB
User avatar
jongjungbu
Not JongJongBu
 
Posts: 3112
Joined: Jun 19, 2001 @ 4:22am
Location: USA


Postby fzammetti » Feb 24, 2004 @ 12:10am

Yes, it seems to do the trick for me too. I found the _tfopen function too. I suppose this all only really becomes and issue if you want to transfer files from platform to platform. I mean, if I write an int out on a given device, and I read it back in later, I don't see how it's endedness matters, or what actually gets written, or what the size of an int is (assuming it wouldn't matter otherwise of course). As long as the value of my int after I read it back in matches the int I wrote out, the rest I don't hink matters much.

Thanks guys, that made my code a lot easier to look at!
...and so I said to Mr. Gates: "$640 billion should be enough for anyone!"
User avatar
fzammetti
pm Insider
 
Posts: 1496
Joined: Jun 4, 2002 @ 6:21pm
Location: Omnytex Technologies


Postby jongjungbu » Feb 24, 2004 @ 1:30am

User avatar
jongjungbu
Not JongJongBu
 
Posts: 3112
Joined: Jun 19, 2001 @ 4:22am
Location: USA


Postby mlepage » Feb 24, 2004 @ 2:28am

www.scalenesoftware.com
Great games for your Palm and Pocket PC!
User avatar
mlepage
pm Insider
 
Posts: 1050
Joined: Aug 3, 2003 @ 4:47am
Location: Canada


Postby angedelamort » Feb 24, 2004 @ 3:37am

User avatar
angedelamort
pm Member
 
Posts: 304
Joined: Mar 7, 2002 @ 11:16pm
Location: Montreal, Quebec


Postby fzammetti » Feb 24, 2004 @ 5:25am

...and so I said to Mr. Gates: "$640 billion should be enough for anyone!"
User avatar
fzammetti
pm Insider
 
Posts: 1496
Joined: Jun 4, 2002 @ 6:21pm
Location: Omnytex Technologies


Postby mlepage » Feb 24, 2004 @ 7:16am

www.scalenesoftware.com
Great games for your Palm and Pocket PC!
User avatar
mlepage
pm Insider
 
Posts: 1050
Joined: Aug 3, 2003 @ 4:47am
Location: Canada


Postby Peter Balogh » Feb 25, 2004 @ 8:36am

Download high quality games for your pda: www.pdamill.com
User avatar
Peter Balogh
pm Insider
 
Posts: 163
Joined: Dec 19, 2001 @ 7:54pm
Location: PDAmill HQ


Return to Windows Mobile


Sort


Forum Description

A discussion forum for mobile device developers on the Windows Mobile platform. Any platform specific topics are welcome.

Moderators:

Dan East, sponge, Digby, David Horn, Kevin Gelso, RICoder

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum