Data File Questions

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jnmediaGAd195
Posts: 58
Joined: Mon Dec 05, 2011 5:35 pm

Data File Questions

Post by jnmediaGAd195 » Sun May 13, 2012 4:11 am

I have six preferences (all numbers) that I want to save in my app.

1. If the file doesn't exist, does it get created when I open it in my app?
2. How do I read each value from the file? I was going to use a CSV file. I was going to store each value in a different variable while the app was running, then update the file when the app is exited.
3. How do I update a specific value?

Any help is appreciated (as always).

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Data File Questions

Post by bangkok » Sun May 13, 2012 10:44 am

Hum... there are many different ways to do what you want to do.

Here are a few ideas :

1 - If the file doesn't exist, does it get created when I open it in my app?
No... unless you want to. You will get an error message into the "the result".
And for writing, you should not worry about it, because when the app will write the preference file, it will create it even if the file doesn't exist previously. No need a special command.

Code: Select all

put URL "file:C:/MYAPP/MYPREF.TXT" into myFile 
if the result is not empty then answer "Pref file is missing"
So I would say : don't bother. Just load the file when you open the stack. If it doesn't exist, set you preferences variable with defaut value.

Code: Select all

put URL "file:C:/MYAPP/MYPREF.TXT" into myFile 
if myFile is empty then
put "1" into myPreference[1]
put "2" into myPreference[2]
put "3" into myPreference[3]
put "4" into myPreference[4]
put "5" into myPreference[5]
put "6" into myPreference[6]

else

--- read the file content

end if
2. How do I read each value from the file? I was going to use a CSV file. I was going to store each value in a different variable while the app was running, then update the file when the app is exited.

It depends on the item delimiter you want to use. You can use ";" but also tab, etc.

You have 6 values, therefore 6 items

Code: Select all

put URL "file:C:/MYAPP/MYPREF.TXT" into myFile 

if myFile is not empty then
set itemdelimiter to tab
repeat with i = 1 to 6
put item i of myFile into  myPreference[i]
end repeat

end if
It will put the 6 values into an array

After, you can access let's say value number 2 by doing

Code: Select all

put mYPreference[2] into abc
If you don't like array, you can use 6 different variables (dont forget to make them "global"):

Code: Select all

put URL "file:C:/MYAPP/MYPREF.TXT" into myFile 
set itemdelimiter to tab
put item 1 of myFile into myPref1
put item 2 of myFile into myPref2
put item 3 of myFile into myPref3
put item 4 of myFile into myPref4
put item 5 of myFile into myPref5
put item 6 of myFile into myPref6
Before your app quits, you save the preferences into the file :

Code: Select all

put empty into myFile
repeat with i = 1 to 6
put myPreference[i]&tab after myFile
end repeat

put myFile into URL "file:C:/MYAPP/MYPREF.TXT"

Jellicle
Posts: 453
Joined: Thu Feb 24, 2011 11:07 am

Re: Data File Questions

Post by Jellicle » Sun May 13, 2012 12:46 pm

Code: Select all

on writefile fileTowrite,dataTowrite
   set the defaultFolder to specialFolderPath("Documents")
   put dataTowrite into URL (("file:"&fileTowrite&".txt"))
end writefile

function readFromFile whatFile
   set the defaultFolder to specialFolderPath("Documents")
   return URL (("file:"&whatFile&".txt"))
end readFromFile
Those routines will help you write and read a file from the documents directory in iOS.

So to write your preferences, just do this the first time your app runs:

Code: Select all

writefile "prefs", "0,0,0,0,0,0"
To change a preference setting, do:

Code: Select all

put readFromFile ("prefs") into thePrefs
put "1" into item 3 of thePrefs -- changes the third item
writefile "prefs", thePrefs

Cheers

Gerry
14" MacBook Pro
Former LiveCode developer.
Now recovering.

jnmediaGAd195
Posts: 58
Joined: Mon Dec 05, 2011 5:35 pm

Re: Data File Questions

Post by jnmediaGAd195 » Sun May 13, 2012 12:59 pm

Thanks for the help. I appreciate it.

Post Reply