PocketMatrix forums

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

Another file saving question

I've been writing my game's saved files using standard ftprintf as shown below...

Code:
_ftprintf(fp,TEXT("%i:"), iVal1);
_ftprintf(fp,TEXT("%s:"), sData1);
_ftprintf(fp,TEXT("%i:"), iVal2);


But I noticed that when I call this for like a thousand times (because there's too much data to save), it saves too long (like 20-30 secs) especially when on a storage card.

Questions:
(1) Are there other ways of saving faster (when you need to save a lot of data)?

(2) Is there a way to assemble all the contents into a larger variable first and then call _ftprintf only once to save the data to file after all the contents have been assembled?

Thanks in advance!
Look up the Win32 file API: CreateFile() and WriteFile(). This is as fast as it can get. ftprintf() is certainly not the best thing to use for speed.
I built a MemoryStream class that you can write to in chunks (modelled on Delphi's TMemoryStream), and then have a File class that wraps the CreateFile etc as Thierry suggested to dump it to a file and it works well even for large saved games.
Do you have to convert your integers to strings to save them? More specifically, must you save your data in a human-readable format?

Dan East
The following approach to file writing was suggested to me some months ago, and it has served me well, maybe it will you too:

Code:

struct THEFILE {
int theFileVersion;
int field1;
TCHAR[32] someStr;
};

void writeFile() {
TCHAR srcStr[10] = TEXT("test_str");
TCHAR fileName[150] = TEXT("");
_tcscat(fileName, filePath);
_tcscat(fileName, TEXT("data.dat"));
THEFILE theFile;
theFile.fileVersion = 2;
theFile.field1 = someData;
_tcscpy(theFile.someStr, srcStr);
FILE* file = _tfopen(fileName, TEXT("w"));
fwrite(&theFile;, 1, sizeof(THEFILE), file);
fclose(file);
}


This assumes you have filePath properly loaded with the path you want to save to (I have a function to get the path of the executable for instance).

Reading is just as easy:

Code:

void readFile() {
TCHAR srcStr[10] = TEXT("test_str");
TCHAR fileName[150] = TEXT("");
_tcscat(fileName, filePath);
_tcscat(fileName, TEXT("data.dat"));
FILE* file;
THEFILE theFile;
if ((file = _tfopen(fileName, TEXT("r"))) == NULL) {
// File not found, probably want to create it
}
fread(&theFile;, 1, sizeof(THEFILE), file);
fclose(file);
// theFile structure is now loaded with data
}


I can't say for sure how this is performance-wise, so you are on your own to see if it suits your needs, but I think it meets your other requirement of assembling everything first.
That's about as good as it gets performance-wise. The only downside is storing fixed-length strings. If we're talking about lots of records then a substantial amount of space could be wasted.

Dan East
Good point. I guess if that's a situation you want to avoid you could just have a char[x] area, and manually fill that in with your strings. I suppose there's something like Java's StringTokenizer that could allow you re-constitute the strings on read?
If I need to store dynamic length strings in a file then I write a short indicating the length of the string, then the string (sans null terminator). To deserialize the string you read the size, allocate the data to hold the string (plus an extra byte), then fread the string into that new array (not forgetting to manually append the null terminator).

If you just write zero terminated strings to file then you have to first load them into a temporary buffer (so you can figure out the length), then allocate the actual buffer, then copy that over.

Dan East
True. I was kind of assuming there was some sort of tokenizer I don't know about that would work in such a case. Assuming it could tokenize on the null, that would save the extra work. In the absence of that, your approach probably makes the most sense (although writing such a tokenizer might not be a bad thing!).
Size then data is definitely a good format for reading (and therefore writing) variable length data.

Binary formats are faster and more compact than human-readable formats. If you are careful you can avoid portability issues.

The first most obvious thing you can do is compose your multiple fprintf calls into one fprintf call.

The next thing you can do is assemble data into a memory buffer (e.g. using sprintf) then fprintf the result all at once. (Though if fprintf is buffered, this shouldn't improve performance, should it?)

If you use platform-specific calls (e.g. Windows or Palm functions) you obviously give up portability.

You'll find that even fprintf isn't portable (e.g. not available on Palm) but at least it's easier to port.
Thanks for the tips guys!

It doesn't have to be in human readable format. Once writing to file is complete, I compress them afterwards.

I stored the data separated by colon (as you can see from the sample above) because they are tokenized when read or loaded (as what Frank has mentioned). I guess one way is to assemble all strings into one long string using stprintf then write it to file. My problem is, there are lines where I exceed 256 characters but the string is only allowed upto 256 characters right?
To what kind of string are you referring? Certainly a straight char[] doesn't have a 256 character limit, and I don't think a TCHAR does (am I wrong anyone?). I'd be surprised if CString had such a limit, although I don't know.
If you are going to use structures like fzammetti suggested then just be careful of alignment issues. I had some trouble that only gave access violations on Dell Axims to do with not having correctly aligned structures. Generally I just put the 32bit values first and the 1byte values at the end.