PocketMatrix forums

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

Reading / Writing to files

Ok, maybe I'm smoking something but I can't seemed to get fwrite, WriteFile, or CFile to properly write the contents of a class to a file....basically, here is what I'm doing for fwrite:

Code:

file = fopen(filename, "wb");

fwrite(&Classname, sizeof(Classname), 1 , file);


What am I doing wrong here? Also I have an array of objects defined for this class. Any help would be appreciated.

btw - names are place holders of course.

thanks,
Greg
Try This:
Code:

file = fopen(filename, "wb");

fwrite(Classname, 1, strlen(Classname), file);


Assuming Classname is an ASCII string, this should work fine. If it is a UNICODE string, you will have to change the second parameter of fwrite to 2.
simonjacobs · 04 May 2002, 10:47 UTC
Maybe I misunderstand, but are you doing:

class Elephant { definition };

Elephant bob;

fwrite(&Elephant;, sizeof(Elephant), 1 , file);

That wont work at all. You need to write the instance of the class (bob) to a file not the abstract definition itself.
Aren't the stdio collection of file stream handlers (fopen, fwrite, etc) not in the stdio library for PocketPCs? I seem to recall the MSDN mentioning they're missing. I thought you had to use Win32's file handlers.
simonjacobs · 04 May 2002, 23:56 UTC
I've been using them for ages with no problems.
No, earlier versions of CE do not support stdio. CE 2.12 may support it, but I know 2.11 (PSPC) and earlier do not.

Dan East
eyespy · 07 May 2002, 19:54 UTC — Thanks for your responses
Thanks everyone for your responses. I ended up doing it member by member...not exactly how I'd like to do it but it works and it's just for save games anyways.

thanks again,
Greg
If the data you're storing is fairly simplistic and static (IE not variable length), then just define a struct with all the data to read / write, and declare a member of that type. Then you can read and write the whole chunk at one time.

Dan East