GP2X Writing To The Sd


dockthepod

Member
Joined
Jun 15, 2006
Messages
250
I'm having a hard time writing to the SD. It creates the file, but it's empty. I gather that I need to sync the SD card somehow, but I haven't been able to figure out how. My code works fine in windows.
 
always, always, umount before removing an sd card! you shouldn't remove the card whilst an application is running from it, and yes, if you want to ensure your program finished writing to the card before continuing, always sync() after fclose() or whatever.
 
Hmm... I had tried the sync() thing last night (from C++) on a whim and it didn't seem to work. However, I did this right before I quit my game rather than right after I closed the file.

It could also be that the file was screwed up. I tried deleting the file from the gp2x explorer program and it would say that it deleted it, but then it would still be there. Then I tried to delete from windows (mounted through gp2x... don't have an sd reader) and it seemed to show up again after I deleted it.

So hopefully the sync() thing is ok and I just need to wipe out that file for reals.
 
When you open the file to write it what functions are you using (fopen, etc ...) and what options are you passing ("w", etc ...). Waiting to sync it after closing is the correct thing to do and when you sync it after the close should not make a difference since the sync just tells the system to write out any unwritten disk buffers. These buffers could be file buffers and/or file system buffers so if you don't the file could be missing data or the file system could loose track of pieces of the file or other file information data.
 
I've been playing around with this at work. I use C++ open and closes.

This works... if I stick a highscore file that I created in windows... I see it fine.

Code:
	ifstream hiscore(FILENAME);
	if(hiscore.is_open())
	{
		hiscore >> topScore;
		hiscore.close();
	}


This is where things go wrong... It turns out that I'm failing the open (I just added that exit for testing). I'm not sure why. Is it possible that the path changes at some point?

Code:
if( theWorld.GetScore() > topScore )
{
 	topScore = theWorld.GetScore();
	ofstream oscore;
	oscore.open(FILENAME, ios::trunc);
	if(!oscore)
		exit(0);	
	else
	{
		oscore << topScore;
		oscore.close();
		sync();
	}
}

It's very weird... and like I said, this works fine on windows.
 
Remember that the file "path" or name is case sensitive in the GP2X and the folder separators "slant" the "right" ('/') way in GP2X and the "left" ('\') way in windows so try and be sure your file name strings are appropriate.
 
I'm actually not using any path information in FILENAME. I was just expecting read/writes without a path to happen in the directory that the gpe resides. Maybe that's the problem.

#define FILENAME "highscore.dat"
 
well I "think" the default directory is where the gpe is but it should at least be where any startup script is. Do you always have a high score file? if not the initial open will fail if you open it for reading otherwise you need to open it for writing to "create" the file or "append" to add on to the file. Generally you would open it read at the start of your program read the data and close it, play the game and then open it for write at the end to re-write the data. You may have already known this so I apologize if this is too simplistic.
 
So I think my program is just fine. I think it's my SD that's messed up. Someone else tried my game and the saving was working just fine.

I think that maybe gmenu2x messed things up, or at least was affected by the same problem. When I create links in there it, it ends up writing out 0 byte files, just like my app is. When I try to delete these from windows, the files are locked and I can't delete them. This all happened before I added saving to my app and after I installed gmenu2x. Maybe it's just a coincidence, but a format is in order I believe.
 
So I think my program is just fine. I think it's my SD that's messed up. Someone else tried my game and the saving was working just fine.

I think that maybe gmenu2x messed things up, or at least was affected by the same problem. When I create links in there it, it ends up writing out 0 byte files, just like my app is. When I try to delete these from windows, the files are locked and I can't delete them. This all happened before I added saving to my app and after I installed gmenu2x. Maybe it's just a coincidence, but a format is in order I believe.

You might want to try to have windows check the file structure and see if the structure got messed up and if it is have windows fix it. Or just reformat the thing FAT32 either way you are sure that the fie structure is not messed up.
 
Last edited by a moderator:
Yup! I had windows check and repair the card and now my save stuff is working! w00t!!! I'll try to get a new release of grow out later today.
 
Back
Top