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
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.
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. ;)
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.
There's no reason to use DOESFILEEXIST with "ini" files.
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)
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 :-)
indeed it seems to convert automatically
Just to share back incase someone else is looking for the info here is my quick test code
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 :)
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.
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
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.
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.
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.
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?
For writing to iPhone, you must use the directory: PLATFORMINFO$("DOCUMENTS"), though. The rest is read only.
Quote from: Kitty Hello on 2010-Jan-14
For writing to iPhone, you must use the directory: PLATFORMINFO$("DOCUMENTS"), though. The rest is read only.
So with my example would I need to have the file name as something like
INIOPEN PLATFORMINFO$("DOCUMENTS")+"numberofruns.ini"
or do I need a / before the ini file name?
The quickest way to find out would be to write a program that does
debug PLATFORMINFO$("DOCUMENTS")+"\n"
and see what it displays, then add a "/" if necessary.
I did it the obvious way
SETCURRENTDIR(PLATFORMINFO$("DOCUMENTS"))
INIOPEN "numberofruns.ini"
Under windows it should be wherever your current profile's "My Documents" folder is (Usually either C:\My Documents\[username] , or just C:\My Documents)
Hmm, I think I need some help...or a kick in the head. :giveup:
I can't seem to make this work;
GLOBAL FILE_PATH$ = PLATFORMINFO$("DOCUMENTS")
I just can't make it return a usable document folder path (I'm on Window XP).
If I print FILE_PATH$ it gives me 'NO_DATA'.
Am I doing something wrong?
GLBasic has problems assigning values to global variables at the same time you create them - i.e.
Global [var] = [value]
First thing to try is separating the creation from the assignment
GLOBAL FILE_PATH$
FILE_PATH$ = PLATFORMINFO$("DOCUMENTS")
Other than that I can't be of much help as a test of those two lines gives me the correct result in a Windows test program and I don't have an iPhone.
Crazy. I just tried here at work and it works perfectly on Windows 7.
But at home, on Windows XP, it doesn't. Strange. :doubt:
I tested on v7.242 on windows XP which worked.
MD5, AES and SHA-512 routines are available in the Maths section, if I remember correctly.