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).
Data File Questions
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Re: Data File Questions
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.
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.
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
It will put the 6 values into an array
After, you can access let's say value number 2 by doing
If you don't like array, you can use 6 different variables (dont forget to make them "global"):
Before your app quits, you save the preferences into the file :
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"
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
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
After, you can access let's say value number 2 by doing
Code: Select all
put mYPreference[2] into abc
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
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"
Re: Data File Questions
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
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"
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.
Former LiveCode developer.
Now recovering.
-
- Posts: 58
- Joined: Mon Dec 05, 2011 5:35 pm
Re: Data File Questions
Thanks for the help. I appreciate it.