Creating a text file isn't working for me

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

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

Re: Creating a text file isn't working for me

Post by Klaus » Mon Mar 08, 2021 7:44 pm

Hi Tom,
trags3 wrote:
Mon Mar 08, 2021 7:35 pm
Hi Klaus
I guess I'm trying to do something that maybe can't be done.
When the app is sent to a user I was under the impression that the app would be able to generate about 9 or 10 empty txt files that will be used when the app is running.
this CAN be done, but I hope you will agree that THIS:

Code: Select all

 put URL("file:" & (specialFolderPath("Documents") & "/PC.txt")) into tfile
does NOT create a file! 8)

This will:

Code: Select all

...
repeat with 1 = 1 to 10
  put EMPTY into url("specialfolderpath("documents") & "/template number" & i & ".txt")
end repeat
...
Et voila, 10 empty text files.
trags3 wrote:
Mon Mar 08, 2021 7:35 pm
...
As they enter data on a card it is saved in a field on the card. When they skip to the next card the data in the fields get saved in a text file. If the user quits the app before completing all pages of input, those files are used to repopulate the cards with the data they had entered during the prior use of the app.
So why not write directly -> on closecard?

Code: Select all

on closecard
  put fld "whatever field you need to write...." into URL("file:" & (specialFolderPath("documents") & "/datafromcard42.txt"))
end closecard
Then on pre-/opencard check if a file for this data is present, see above, and read that file into your field(s).

Best

Klaus

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Creating a text file isn't working for me

Post by trags3 » Mon Mar 08, 2021 8:01 pm

I think what I need to use is the saveStackRequest statement.
Trying it out now.
Tom

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Creating a text file isn't working for me

Post by SparkOut » Mon Mar 08, 2021 8:07 pm

The saveStackRequest message will only be generated if something happens to trigger a save event. Do you have a statement to "save this stack" anywhere in your code? If not, your handler will never fire.
Using stack save messages to save your card states is one way to do it, but not very robust and relies on specific intervention.
Why not use the closeCard script as suggested above? This will do what you need succinctly.

stam
Posts: 3073
Joined: Sun Jun 04, 2006 9:39 pm

Re: Creating a text file isn't working for me

Post by stam » Tue Mar 09, 2021 1:09 am

trags3 wrote:
Mon Mar 08, 2021 7:35 pm
I guess I'm trying to do something that maybe can't be done.
When the app is sent to a user I was under the impression that the app would be able to generate about 9 or 10 empty txt files
Hi Tom,
as bogs, klaus and others mention, your code is incorrect, because you're putting a file path string into a variable, not creating a file. To create a file, simply put text into a URL file path and it should be created.

To create the empty text file PC.txt in your documents folder, the code is

Code: Select all

put "" into URL("file:" & (specialFolderPath("Documents") & "/PC.txt"))
I ran this from the messageBox and it created an empty text file called PC.txt in my Documents folder.
Is that what you're trying to do, or have i misunderstood?

Stam

Post Reply