File handling question

Previous topic - Next topic

Gary

Im having a stupid moment and cannot get my head around how the best way to read and write a file using GLbasic for an iPhone app

Basically on start up I want to read a file in that contains things like high score and game stats. Also I want to generate a random string the first time the app is run to identify the users iPhone for an online high score chart.

What I would like to know is whats the best way to check if a file exists, create the file if it doesnt exist (this would be done on first run only), read in the file, copy the variables to the game at run time and to write the variables back out at the end of each game.

Some of the data will be stored in an array, other will just be standard variables and strings

If anybody has any example code to do this and you can share it then it would be much appreciated

Thanks
Gary

doimus

I assume that standard file-handling works on iPhone - if that's not the cause, excuse me.
Anyway, have you checked the OPENFILE() in the manual? It states everything that's needed for file I/O in GLB.
If you still don't get it from the help, try looking for file I/O tutorials in C, as it is very similar to how GLB does it.

matchy

There no difference for iPhone so INIOPEN, INIGET$, INIPUT$ are the easiest ways to store new and old local data, and retreive local data. NETWEBGET$ is the simplest for uploading and downloading remote data. ;)

Hemlos

Good replies, here is one more..

ok=DOESFILEEXIST("filename.ini")
If file exists, then ok=1

Other than that, you can use the "ini" or "write" command sets, documentation can be found in the help file index, and examples are in the GLBasic sample projects directory.

Bing ChatGpt is pretty smart :O

matchy

There's no reason to use DOESFILEEXIST with "ini" files.

Gary

Thanks for the replies so far, they all make sense and an ini file does look to be the best way to go. One other quick question, all the examples for iniput and iniget$ refer to strings rather than numerical values, can I use numerical values or will I have to convert the string back to a number (no great problem if I do, just an extra routine)

Moru

It's all auto-converted to fit into the variable I think.

a$ = 5
b = "5"

should both work. But I have been programming too much PHP lately so I can have something wrong there :-)

Gary

indeed it seems to convert automatically

Just to share back incase someone else is looking for the info here is my quick test code

Code (glbasic) Select
INIOPEN "numberofruns.ini"
runs=INIGET$("data","val1")
name$=INIGET$("name","myname")
IF(name$="NO_DATA") THEN name$="Gary"
runs=runs+1
INIPUT "data","val1",runs
INIPUT "name","myname",name$
PRINT runs,0,0
PRINT name$,0,10
SHOWSCREEN
INIOPEN ""


this will open a file called "numberofruns.ini" and if it doesnt exist it creates it for you (hence the reason you dont need to check it exists I guess)
It then reads in 2 values, first is number of times the program has been run and second is a name string. If the file has just been created then run will contain 0 and name$ will contain the string "NO_DATA"
It then adds 1 to runs and sets the name to "Gary" if it isnt already
then it updates the 2 stored values
prints the output and then shuts the file down

Its so simple, much easier than trying to use a file and pulling the data from inside the file

Now just to work on the NETWEBGET$ bit :)

matchy

Tip: in one line of code, it is much easier to submit highscores in a URL WEBNETGET$ like for example:

"website/hiscoretable.php?name=joe&score=1234&add=1&secretcode=AE3D5"

rather than to submit it in html POST form.

Gary

thanks matchy, that was the way I was planning of doing having worked a fair bit with php. But always nice to know I'm heading down the simplest route before I start on the complex stuff :)

Once again the versitility of GLBasic shows no limits. It really is the simplest way I have found of producing games on the iPhone and probably the best set of forums I have seen on any site

Moru

Quote from: matchy on 2010-Jan-13
Tip: in one line of code, it is much easier to submit highscores in a URL WEBNETGET$ like for example:

"website/hiscoretable.php?name=joe&score=1234&add=1&secretcode=AE3D5"

rather than to submit it in html POST form.

This also makes it incredibly easy to fake your score :-) See my homepage for a slightly harder version to fake, using url transfer.

matchy


Moru, just an easy tip and the "secretcode" was to demonstrate low security for Gary_Leeds.
I did not know about your signature test. Looks great and something that I will check out soon.

FutureCow

If you used an encryption function to generate the secret code based on the score and username, it would be a lot harder to fake.

Moru

That's exactly what my routine does. It's no real encryption since I couldn't figure out how they were made but there won't be any script kiddies doing it :-)

However I remember seing someone porting AES on the boards I think?

Kitty Hello

For writing to iPhone, you must use the directory: PLATFORMINFO$("DOCUMENTS"), though. The rest is read only.