GP32 File Io


Akuma no Houkon

Certified Guru
Joined
Mar 4, 2004
Messages
1,194
Age
44
Location
USA > Washington > Everett
Website
akuma.gp32news.com
Using the SDK to open and read from a file on the SMC, if I open it / read from it / close it about 10 times, it stops opening.

Is there a reason for this? Some SMC card limit? All other games/apps I have used work fine on the SMC reading multiple files multiple times...

This is what I do; I have a large AllMyLevels.dat file, I open the file, seek to the spot that the level the player is on is located, and read that data into my arrays. I then close the file. After level 10 it stops working, (or if I load level 1 ten times), if I start on level 10, it will load upto level 20, etc... after opening, reading, and closing the file 10 times (_always_ 10 times) it stops working.

Now the obvious work arround for this is to open the file at the start of the program and close it at the end. I do that and it works just fine for this situation (assuming it doesnt cause problems on the SMC if you shut off the GP32 without the file being closed...), but I have other situations where this method is not an option.

Does anyone know why this is happening?
 
Don't know what you are doing? would be helpfull if you provided the source to the problem code! Here is an example I use. It may fix your problem?

// Initalise the fat system.
GpFatInit();

// Set a relative path.
if ( GpRelativePathSet( "gp:\\gpmm" ) != SM_OK ) {
GpTextOut( NULL, &gpDraw[nflip], 0, 0, "ERROR SETTING PATH", 0x0 );
return FALSE;
}

//
if ( GpFileCreate("logit.raw", NOT_IF_EXIST, &h_file ) != SM_OK ) {
GpTextOut( NULL, &gpDraw[nflip], 0, 0, "ERROR CREATING FILE", 0x0 );
return FALSE;
}

if ( GpFileOpen( "logit.raw", OPEN_W, &h_file ) != SM_OK ) {
GpTextOut( NULL, &gpDraw[nflip], 0, 0, "ERROR OPENING FILE", 0x0 );
return FALSE;
}

// Do something here ???

//
GpFileClose( h_file );
 
Don't know what you are doing?
I stated it clearly, I Open, Seek, Read, Close. Thats all, nothing to it.

Again, in "code language" if you still dont get what I do:
Code:
GpFileOpen(fname,OPEN_R, &OpenedFile);
GpFileSeek(OpenedFile, FROM_BEGIN, offset, oldoffset);
GpFileRead(OpenedFile,datptr, datlength,&readbytes);
GpFileClose(OpenedFile);
(if you see any mistakes in this code, its probably because this isnt the real code I use, that no longer exists in my project, I made work arrounds for now, so this is just a code mockup)

Of course I GpFatInit(); at the begining of the app and everything is its proper type

And it all works fine for the first 10 times, then it stops working. If I comment out the open and close, and put open before and close after, my main loop then everything works just fine. But the SAME CODE opening and closing the file each time, only works 10 times.
 
just dont open/close your file...

open it at the app init
seek/read as much as you want in the app
and close the file at the app end

reseting gp32 with opened files isnt an issue for RO files, but for RW files you'll loose the modifications

explain your 'other situations where this method is not an option'

btw: i think its an GP' SDK limit
 
There are many times where this isnt an option, the ones that come to mind right now, are the files that you dont know will be opened, say, a NotePad app, image viewer, or a MP3 player of sorts, etc..., apps that allow the user to select and open files by their choice.

Also, for instance, as you stated, RW files must be closed to update, so we have games that alow the user to save, be it their game, highscores, settings, SRAM, save states, etc... the file must be closed to update, and since its closed, you have to reopen it again the next time the user saves state or changes settings, etc...

Or maybe you have a very large and modular game, that has say 300+ files that you dont want to shove into one large file, opening 300+ files at once is just a bit overboard dontcha think? (sometimes a large file and seeking is not the optimal way, its difficult to compile into a large file and harder to update small things, etc..)



Can anyone verify if this is a SMC limit, an SDK limit, a GP32 limit, or if something is just not working right in my code?
 
It's not an SDK limitation 'cos yesterday I opened and closed over 100 files in succession. My guess would be a memory leak somewhere else in your code.
 
In my VNS there is a constant loading of sprites and backgounds (only the last used is cached). So in some stories the same sprite/backgound is opened far more then 10 times without problems.

The only difference I can spot with your problem is that in VNS the same file isn't opend ten times in a row but alternated with other files. So open file1, open file2, open file1, open file3, open file1 etc.
 
Hiya, im having exactly the same problem - its not a memory leak as i monitor the ram used after every save and it doesn't change a byte.. and my program is loading different files in succession :p maybe its a library bug in the version we have or something.

Akuma, are you creating the files everytime you want to open and write to them? cos i took out the 'create' function and it let me save it more than 10 times, maybe we can work around it with that.
Code:
	(GpFileOpen("COL.DAT", OPEN_W, &h_file) != SM_OK) {
	GpFileCreate("COL.DAT",ALWAYS_CREATE,&h_file);
	GpFileOpen("COL.DAT", OPEN_W, &h_file);
	}
for example
 
I tried both opening / seeking / reading / closing an existing file (not creating a new one) and it did this. It also does this when I open / write / close 10 times.

I did not find a work arround, however, in one instance I was able to open the file at the begining of the game and close it at the end.

I dont know if its an SDK problem, a code problem, or a SMC problem, I would assume its not a code problem as yours does it the exact same, with the exact same amount of times (10). So this makes it an SDK issue or an SMC issue. Have you tried using Mirko's SDK and checked to see if that made any difference?
 
I fixed it mate,

its in the GPFileCreate function which manifests the bug, and only once it manifests does the count down start, which explains why i could load a map 20 odd times but once i saved it, and loaded 9 times it crashed :D

anyway, i took out every GpFileCreate and made it only create the file if the opening failed, like so..

if (GpFileOpen("ATK.DAT", OPEN_W, &h_file) != SM_OK) {
GpFileCreate("ATK.DAT",ALWAYS_CREATE,&h_file);
GpFileOpen("ATK.DAT", OPEN_W, &h_file);
}

that isnt the exact code i use, as i left the Error reporting on the ones inside the bracket but figured you get the idea.

just been hammering the game, loading 50 times, changing map 50 times, trying my best to get it to crash - without any problems, so it looks like that sorted it (fingers crossed)

i'll keep you informed incase it occurs again!

p.s make sure your closing all open files before opening new ones, as i had a similar bug before where i had accidently forgotton to close a couple of files so eventually the ram ran out and it crashed.
 
yup, coded it two days ago and spent all of yesterday just debugging it, it had about 5 serious SMC bugs at one stage, and just as i'd fix one, i'd find another :D

but today i managed to play around and make a map without a single glitch, so hopefully i can move onto coding more stuff now.
 
Back
Top