read/write files in a standalone app

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
tyarmsteadBUSuSfT
Posts: 151
Joined: Sat Aug 25, 2012 1:14 am

read/write files in a standalone app

Post by tyarmsteadBUSuSfT » Sun Jan 13, 2013 6:18 pm

I have a file that I want the user to be able to save info to and read it in field. My problem is that I'm using the specialfilefolder, and every thing works in the simulator, but when I build the standalone and make ad save entries the list of saved entries does not display.
Thank you

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: read/write files in a standalone app

Post by Klaus » Sun Jan 13, 2013 6:22 pm

Hi tyarmsteadBUSuSfT,

the actual name is "specialfolderpath()" and it is a function 8)
What syntax are you using? Please post your code.


Best

Klaus

tyarmsteadBUSuSfT
Posts: 151
Joined: Sat Aug 25, 2012 1:14 am

Re: read/write files in a standalone app

Post by tyarmsteadBUSuSfT » Sun Jan 13, 2013 8:57 pm

I apologize, missed type in my post.

put url ("file:" & specialFolderPath("documents") & "/colorlist.txt") into tSavedFile
the colorlist.txt is the file that is created, read and written to.

Thank you

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: read/write files in a standalone app

Post by Klaus » Sun Jan 13, 2013 9:49 pm

Hi,

OK, the script is correct, how do you write the data in your standalone?
Please post that code, too :D

But wait, maybe this will only fail the first time when there is NOT YET a file with this name?
So do some checking in your scripts:

Code: Select all

...
put specialFolderPath("documents") & "/colorlist.txt") into tFile
if there is a file tFile then
  put url ("file:" &  tFile) into tSavedData
else
  put EMPTY into tSavedData
end if
...
Best

Klaus

tyarmsteadBUSuSfT
Posts: 151
Joined: Sat Aug 25, 2012 1:14 am

Re: read/write files in a standalone app

Post by tyarmsteadBUSuSfT » Sun Jan 13, 2013 11:43 pm

on mouseUp
ask info "Save As Name:" titled "Save Entry"
if it is empty then exit mouseup
put it into tSavedname
if tSavedname is "" then answer error "Must Enter a file Name"
if tSavedname is empty then exit mouseUp
set the itemDel to tab
open file url ("file:" & specialFolderPath("documents") & "/colorlist.txt") for write
put url ("file:" & specialFolderPath("documents") & "/colorlist.txt") into tSavedFile
put the number of lines in tSavedFile + 1 into tNextFileNumber
put tSavedName into item 1 of line tNextFileNumber of tSavedFile
put item 1 of field "Color2" into item 2 of line tNextFileNumber of tSavedFile
put item 2 of field "Color2" into item 3 of line tNextFileNumber of tSavedFile
put item 1 of field "Color3" into item 4 of line tNextFileNumber of tSavedFile
put item 2 of field "Color3" into item 5 of line tNextFileNumber of tSavedFile
put item 1 of field "Color4" into item 6 of line tNextFileNumber of tSavedFile
put item 2 of field "Color4" into item 7 of line tNextFileNumber of tSavedFile
put item 1 of line 1 of field "Color" into item 8 of line tNextFileNumber of tSavedFile
put item 1 of line 2 of field "Color" into item 9 of line tNextFileNumber of tSavedFile
-- put return
put tSavedFile into URL ("file:" & specialFolderPath("documents") & "/colorList.txt")
close file url ("file:" & specialFolderPath("documents") & "/colorlist.txt")
--put url ("file:" & "/colormatchlist.txt") into tfile
answer info "Entry Saved"


end mouseUp

I just recently added the open and close file options
Thank you for your help
Ty

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: read/write files in a standalone app

Post by Traxgeek » Mon Jan 14, 2013 7:59 am

Hi tyarmsteadBUSuSfT,

I'm a complete Newbie here but do you 'NEED' the 'file:' before the 'specialfolderpath.....' in your code ?

So, in theory,
open file url ("file:" & specialFolderPath("documents") & "/colorlist.txt") for write
would be come (in my rather simple world !)
open file url (specialFolderPath("documents") & "/colorlist.txt") for write

Hope I'm not being stupid here ! If I am - just ignore me ! :?

Rgds Traxgeek
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: read/write files in a standalone app

Post by Klaus » Mon Jan 14, 2013 12:59 pm

Hi Ty,

Traxgeek is correct, wrong syntax!
You did not add the "if there is a file XXX" check as suggested?!

And your are mixing the syntax for "open file XXX for read" with the shorter "url("file:...") syntax!
Decide to use ONE of them!

Try this:

Code: Select all

on mouseUp
  set itemdel to TAB
  
  ask info "Save As Name:" titled "Save Entry"
  put it into tSavedname
  if tSavedname = empty then
    answer error "Must Enter a file Name"
    exit mouseUp
  end if
  
  ## Put complete path into a variable, will save a lot of typing later:
  put specialFolderPath("documents") & "/colorlist.txt"  into tFile
  
  ## IMPORTANT!!!!
  ## If there is not a file then the script will HALT at this point and might leave you clueless!
  if there is a file tFile then
    
    ## Put content of file "en bloc" into variable!
    put url ("file:" & specialFolderPath("documents") & "/colorlist.txt")  into tSavedFile
    
    ## Plan B:
  ELSE
    put EMPTY into tSavedFile
  end if
  
  ## Fill new content of variable
  put the number of lines in tSavedFile + 1 into tNextFileNumber
  put tSavedName into item 1 of line tNextFileNumber of tSavedFile
  put item 1 of field "Color2" into item 2 of line tNextFileNumber of tSavedFile
  put item 2 of field "Color2" into item 3 of line tNextFileNumber of tSavedFile
  put item 1 of field "Color3" into item 4 of line tNextFileNumber of tSavedFile
  put item 2 of field "Color3" into item 5 of line tNextFileNumber of tSavedFile
  put item 1 of field "Color4" into item 6 of line tNextFileNumber of tSavedFile
  put item 2 of field "Color4" into item 7 of line tNextFileNumber of tSavedFile
  put item 1 of line 1 of field "Color" into item 8 of line tNextFileNumber of tSavedFile
  put item 1 of line 2 of field "Color" into item 9 of line tNextFileNumber of tSavedFile
  
  ## Write new content back to file:
  put tSavedFile into URL ("file:" & specialFolderPath("documents") & "/colorList.txt")
  
  answer info "Entry Saved"
end mouseUp
Do you see the last dialog "Entry saved"?

Best

Klaus

tyarmsteadBUSuSfT
Posts: 151
Joined: Sat Aug 25, 2012 1:14 am

Re: read/write files in a standalone app

Post by tyarmsteadBUSuSfT » Tue Jan 15, 2013 12:36 am

Thank you, but it is still not working in the standalone version. I copied your code into my app. When I go to the card with the list of saved files I use this:
on opencard
put url ("file:" & specialFolderPath("documents") & "/colorlist.txt") into tSavedFile
repeat with x =1 to the number of lines in tSavedFile
put item 1 of line x of tSavedFile into item 1 of line x of field "ColorList"
end repeat
end opencard

It works fine on the desk top and simulator.
Thank you for your help and patience.
Ty

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: read/write files in a standalone app

Post by Klaus » Tue Jan 15, 2013 12:36 pm

Hi Ty,

add this line after writing of the file, maybe "the result" will give us a hint:
...
## Write new content back to file:
put tSavedFile into URL ("file:" & specialFolderPath("documents") & "/colorList.txt")
ANSWER the result
...

Best

Klaus

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

Re: read/write files in a standalone app

Post by bangkok » Tue Jan 15, 2013 6:14 pm

One question : what do you call "simulator" ? And furthermore, which OS do you use ?

Then, for debugging purpose you should add a few "answer" (along with the result) in order to see how your script is working, and which data the script is handling.

For instance, are you sure that you actually write data to your file ?

Code: Select all

answer tSavedFile 
put tSavedFile into URL ("file:" & specialFolderPath("documents") & "/colorList.txt")
answer the result
For reading :

Code: Select all

on opencard
put url ("file:" & specialFolderPath("documents") & "/colorlist.txt") into tSavedFile
answer tSavedFile
Other issue, in your write script you use "set itemdelimiter to tab"

And then in your read script, you seem to have forgotten it...

tyarmsteadBUSuSfT
Posts: 151
Joined: Sat Aug 25, 2012 1:14 am

Re: read/write files in a standalone app

Post by tyarmsteadBUSuSfT » Tue Jan 15, 2013 8:02 pm

Thank you for the great feedback. I used both answer options and I got "OK" then trying again I used tSavedFile and got the variable displayed as it should.
Ty

Post Reply