Writing to a file in GP2X?

Previous topic - Next topic

BasicMe

I want to have the GP2X write to a file, so that the program can keep track of level progress.

I've tried both the PUTFILE command, as well as OPENFILE / WRITELINE / CLOSEFILE, and neither method will write to a file.

Any suggestions?

Kitty Hello

I've seen someone write such a file, working. Please post your code.
Also, please write to the sd card (and test from there), don't use your nand.

BasicMe

Here's my code.  On the PC, both text files are written fine to the same directory as the executable, no problem.  On the GP2X, nothing happens.  :(

Yes, I do keep all my files on an SD card.
__________

OPENFILE(1, "test1.txt", FALSE)
WRITELINE 1, "1234"
CLOSEFILE 1

PUTFILE "test2.txt", 0, "this is another test file"

waitforselect:
SHOWSCREEN
IF KEY(15)=0 THEN GOTO waitforselect

Kitty Hello


Achim

Well - same at my side - the highscore table for a game will not be saved.

CYA !
- www.softworxs.de -
[Be different - play smart]

Kitty Hello

Anyone with an F100 device here? Can you run from a terminal and post the output text?
I won't be able to fix it until the weekend I fear.

S.O.P.M.

I used the read and write commands often and they were working always fine on the '2X. I'm a F100 User and would really like to help but how about using the terminal with the handheld? I've absolutely no clue.
Notebook PC Samsung E372 Core i5 @ 2,6 GHz; 4 GB RAM; Win 7 Home Premium

Thunor

I tested the code. In fact I ran this :-
Code (glbasic) Select
result = OPENFILE(1, "test1.txt", FALSE)
WRITELINE 1, "1234"
CLOSEFILE 1
PRINT result, 100, 100
SHOWSCREEN
I have an F100 MkII with a 1GB Sandisk SD and I'm using GMenu2X.

It printed 1, I quit the app, enabled USB on SD and I found test1.txt with contents 1234.

I ran the test again but instead of quitting the app I turned the GP2X off. Unsurprisingly test1.txt was not there because the SD card wasn't synced; this is normal.

[EDIT]
I tested the same code under the default GP2X GUI and it worked fine also.

Question: do GLBasic apps sync on exit?

BasicMe

I also have an F100 MkII.  I'm using a Sandisk 2GB SD card, and I'm using the stock GP2X GUI (is this important?).

As before, I quit the game from within the program (I did NOT simply shut the GP2X off).  I even used Thunor's code exactly, in case printing the text had an impact on file saving.  Once the program had quit, I waited about 10 seconds, THEN shut off the unit and popped out the SD card, sliding it into my laptop.

I'm still getting nothing!  :(

Gernot, I'll be happy to help with this, but I also don't understand what running from a terminal is.  I'm very computer-tech illiterate.  Can you give me detailed instructions, or point me to same?

BasicMe

Aha!  I'm narrowing down the problem.  Here's my latest test code:

Code (glbasic) Select
IF DOESFILEEXIST("test.txt")
  PRINT "Test.txt exists", 100, 100
ELSE
  PRINT "test.txt does NOT exist", 100, 100
ENDIF

IF DOESFILEEXIST("test2.txt")
  PRINT "Test2.txt exists", 100, 120
ELSE
  PRINT "test2.txt does NOT exist", 100, 120
ENDIF

OPENFILE(1, "test.txt", FALSE)
WRITELINE 1, "1234"
CLOSEFILE 1

PUTFILE "test2.txt", 0, "this is another test file"

PRINT "press SELECT to exit", 100, 200
SHOWSCREEN

waitforselect:
IF KEY(15)=0 THEN GOTO waitforselect
I tried running this program, I hit "select" to exit, waited 10 seconds, and then turned off my GP2X.  After waiting a few seconds, I turned the unit back on and ran the program again, and neither text file was created.  I repeated multiple times with the same results.

HOWEVER...

I tried running the program again, hit "select" to exit...but instead of turning the GP2X off, I simply waited a few seconds and ran the program again.

This time the "test.txt" file was created fine!

Unfortunately, leaving the GP2X on until the next time you play isn't a practical solution.  But at least this explains why Thunor's test worked fine, and mine did not.

Any suggestions for a workaround fix?

Thunor

Sounds like a syncing issue.

Try this: create a text file called run.txt in the same directory as your game. Put this inside it :-
Code (glbasic) Select
#!/bin/sh
./nameofyourgame.gpe
sync
cd /usr/gp2x
exec ./gp2xmenu
Change nameofyourgame.gpe in the above script to whatever your game is called. Save it and then rename it to run.gpe

Run this instead and see what happens. This is a standard script for running GP2X apps which flushes the file buffers for the SD card when the app has terminated.

BTW you can use the Explorer on the GP2X to navigate to your game folder and check your test files have been created. I don't know if you are aware of this so I mentioned it anyway. Then you won't need to remove the SD card and put it in your laptop.

BasicMe

I created the "run.gpe" file as you suggested Thunor, and unfortunately I still encountered the same issues (and for the record, I did save the file in Unix format).

However, an interesting thing happened when I used the GP2X's Explorer:

If I run the program once, quit it, shut down & restart the GP2X, then the "test.txt" file will not be created.

But if I run the program once, quit it, and go into the GP2X's Explorer, the "test.txt" file is there!  Then I can shut down & restart the GP2X and the file will stay there!

Is this part of that whole syncing issue?

Kitty Hello

OK, I'll add sync on exit. Forgot about that.
Try calling it with SHELLCMD

Thunor

BasicMe:, I did the same tests as you but my file test1.txt was always there. I even turned the GP2X off, took out the card and put it in my Sharp Zaurus but it was still there.

If you're running your game via the run.gpe script instead of directly then the sync in the script should have flushed the file buffers out to the SD.

You can try what GernotFrisch has mentioned above. Add this to the end of your code :-
Code (glbasic) Select
ok = SHELLCMD("sync", TRUE, FALSE, rv)
PRINT "ok=" + ok, 100, 120
SHOWSCREEN
KEYWAIT
END
You should see "ok=1", then press a button and do the same tests you did in your post #12.

Do you get this problem with anything else on the GP2X, i.e. non-GLBasic games and apps?

BasicMe

That did it!  Thanks!!

For the record (for anyone else having this problem), the final code that worked was:

Code (glbasic) Select
OPENFILE(1, "test1.txt", FALSE)
WRITELINE 1, "1234"
CLOSEFILE 1

PUTFILE "test2.txt", 0, "this is another test file"
PRINT "press any key to exit", 100, 200

ok = SHELLCMD("sync", TRUE, FALSE, rv)
SHOWSCREEN
KEYWAIT
END
In this example, the OPENFILE/WRITELINE/CLOSEFILE method works perfectly.  The PUTFILE method still does not work, but that's easily avoided.

Thunor, I don't know why we got different results.  You mentioned earlier that you're using GMenu2X; could that be syncing your SD card after the GLBasic program quits?  I've been using the stock GP2X gui exclusively.

In any case, thanks everyone for your help!!  :)