ok Ben, i have an old copy of CodeWarrior for Mac (but not OS X) and, if you're using C/C++ then let's stick to ANSI for the time being.
I'm assuming you've declared a class like this:[tt]
class MyClass
{
public:
// construction/destruction here...
// member functions here...
// member variables here...
};[/tt]
You could save the data for an object of type class MyClass using the methods I already indicated like so:[tt]
bool SaveData(const MyClass& data)
{
auto FILE* f = fopen("thedata.dat","wb+"

;
if (!f) return (false);
fwrite(&data,sizeof(MyClass),1,f);
fflush(f);
fclose(f);
return (true);
}[/tt]
This function will write the member variables of an an object of type MyClass to a file called 'thedata.dat' - obviously, substitute the class and file names for your specific names.
There are a couple of ways to keep count of the number of objects stored in the file. You could specifically "save" this number at the head of the file (ie. you write the number first then append the object data) OR... you could simply forget about the number and then when you need to read the data back in, you could get the file size and see how many times sizeof(MyClass) divides into it!
To read the data back in you could use a function like this:[tt]
bool ReadData(MyClass& data)
{
auto FILE* f = fopen("thedata.dat","rb"

;
if (!f) return (false);
auto unsigned bytesRead=0;
bytesRead=fread(&data,sizeof(MyClass),1,f);
fclose(f);
if (bytesRead!=sizeof(MyClass) return (false);
return true;
}[/tt]
If you want to write/read several objects (for example, if you have an array of objects of type MyClass) then you can stick a loop around the fwrite() and fread() operations to write/read each object of the array in succession.
If you have an existing data file and you want to add (append) another object on the end of the file you would use one of the 'appending' modes to open the file, eg:[tt]
FILE* f = fopen("filename","ab+"

;[/tt]
There are several other I/O functions that you will find helpful when dealing with files. Each file has what is called a file marker (ie. the read/write position in the file itself). It also has an EOF (end-of-file) marker. The EOF marker is a bit like a NULL terminator of a c-style string! There are a few functions for getting and setting the file marker:[tt]
ftell(); // <- gets the current position of the marker
fseek(); // <- sets the current position of the marker[/tt]
You could move the file marker to the beginning of the file like this:[tt]
fseek(f,0,SEEK_SET);[/tt]
where 'f' is the name of your FILE* - you can set it to the end of the file like this:[tt]
fseek(f,0,SEEK_END);[/tt]
and get the current position like this:
[tt]
fseek(f,0,SEEK_CUR);[/tt]
You can move it 12 bytes from the beginning like this:[tt]
fseek(f,12,SEEK_SET);[/tt]
or... 12 bytes before the end of file like this:
[tt]
fseek(f,-12,SEEK_END);[/tt]
Therefore, you can use these functions to get the actual length of the file (in bytes) like this:[tt]
fseek(f,0,SEEK_END); // move to EOF
unsigned length = ftell(f);[/tt]
Now, say you have a data file which has 8 records of type MyClass written to it and you need to update only one of them, there are two ways you can do this:
You can either create an array to hold all 8 objects and suck them all up from the file into the array then do the change as necessary and then write them all back (the long way!) OR.... you can open the file using "rb+" mode and fread() each object in at a time until you find the record you need to change, make the change to the object, get the current position using ftell(), set the read/write position to the current position minus sizeof(MyClass) by using the fseek() function and then fwrite() the updated object to disk.
All this file reading/writing stuff is real easy once you get your head around it. Try messing around with the basic examples I gave you. If you're using a newer version CodeWarrior then you may need to change the #include header names to the newer versions but everything should still work fine.
If you want to e-mail me your source code then feel free to do so at: nick@qed-online.com
programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.