Page 1 of 1
What's most reliable way to save text to external file
Posted: Mon Apr 07, 2025 11:10 pm
by urbaud
In LiveCode, I want to know which is the best and most reliable and robust way to Save and retrieve (Open) text in a text field from/to an external file. Part of the Save/Open function would be to use htmlText to format the text for size, color and style. Also, where would the ideal place be for the external file to be saved to: Desktop, documents, Preferences, home, engine, etc. Which of the following formats is the best, most reliable way to save and retrieve the text:
1. Open, read/write, Close format
2. Open, read/write binary, Close format
3. URL format
4. URL binfile format
Re: What's most reliable way to save text to external file
Posted: Tue Apr 08, 2025 3:12 pm
by dunbarx
Hi.
I do just a little of this, but basically I simply build a string like "myDestination.txt" and put it into a variable "filePath":
Code: Select all
open file filePath for text write
write dataToWrite to file filePath
close file filePath
I am sure you can enhance this with additional parameters to the command "write".
Craig
Re: What's most reliable way to save text to external file
Posted: Tue Apr 08, 2025 3:16 pm
by dunbarx
Know that if you do not already have an explicit folder to write to, LC. (like HC before it) will create one.
Craig
Re: What's most reliable way to save text to external file
Posted: Tue Apr 08, 2025 3:54 pm
by FourthWorld
dunbarx wrote: ↑Tue Apr 08, 2025 3:16 pm
Know that if you do not already have an explicit folder to write to, LC. (like HC before it) will create one.
When attempting to write to a folder that doesn't exist, LC should note that as an error in the result, rather than create the folder.
If this has changed then this typo:
Code: Select all
put tData into URL ("file:~/Desjtop")
... would succeed even tho we probably don't want it to.
All these years I've had to use "create folder" to create folders I want to use that don't already exist.
Do I misunderstand what you wrote?
Re: What's most reliable way to save text to external file
Posted: Tue Apr 08, 2025 5:53 pm
by Klaus
dunbarx wrote: ↑Tue Apr 08, 2025 3:16 pm
Know that if you do not already have an explicit folder to write to, LC. (like HC before it) will create one.
Craig
No, it doesn't!
When you try, the result will be:
invalid URL: /Users/klaus2/Desktop/notpresentfolder/ttt.txt
Re: What's most reliable way to save text to external file
Posted: Tue Apr 08, 2025 7:19 pm
by SparkOut
@Craig, naybe you are remembering it the other way round? If you script a "create folder" command and a folder already exists, then LC does not return an error.
I am intrigued now and want to test this but can't at the moment.
For the OP, "best" or "most robust" or "optimal" are all subjective. Pretty much all the suggestions you mentioned are equally valid, there might be just the tiniest but of overhead in maintaining the open handles on a file before explicitly closing but for the mentioned task of saving html text from a field into a file, I would say the simpler way would be to put into url ("file:" & thePath), and let LiveCode handle the file system operations without thinking about it.
Where to store the file depends entirely on the expectation of what it is for. "engine" is not a valid location these days, where "resources" should be used instead, but that is not a writeable location on some operating systems (mobile).
If the file is meant to keep data relevant to your app (especially if not meant to be viewed by the user independently of your app) that suggests a "preferences" type location. If it is meant to be data exported so that other apps can access it, then perhaps "documents" but it really depends on what the intention is.
Re: What's most reliable way to save text to external file
Posted: Tue Apr 08, 2025 7:50 pm
by FourthWorld
urbaud wrote: ↑Mon Apr 07, 2025 11:10 pm
In LiveCode, I want to know which is the best and most reliable and robust way to Save and retrieve (Open) text in a text field from/to an external file.
...
1. Open, read/write, Close format
2. Open, read/write binary, Close format
3. URL format
4. URL binfile format
The URL syntax is a shorthand way to accomplish the same underlying operations as open/write/close.
Have you experienced some unreliability with either method? If so, let's examine the code to see where error checking can be added to guide it.
Re: What's most reliable way to save text to external file
Posted: Tue Apr 08, 2025 7:55 pm
by dunbarx
Klaus, Richard. Sparkout.
I misSpoke. The write command will create a new document named from the data supplied to it.
Craig
Re: What's most reliable way to save text to external file
Posted: Wed Apr 09, 2025 9:49 pm
by urbaud
To all of you who replied to my post I want to thank you. The reason I'm asking about this is because I have on occasion, inadvertently, deleted the external file. I don't want that to happen again. I'm a LiveCode hobbyist and do coding to keep my old brain (80 plus yrs.) from declining to fast. I use the file to keep bits and pieces of info that might be needed or interesting in the future. The app I built was to get rid of paper notes and consolidate the info in one place where I could search it.
Also, as an added task for myself and for fun, I asked several AI chat bots the same question. I thought I'd share the simple results with you. See below
Claude = open read/write binary close
Chatgpt = URL not binary
Gemini = open read/write close
Ninja agent = open read/write close
Deepseek = URL binfile
YesChat = URL not binary
Each of them had long explanations for their recommendations. Interesting exercise. Again, thanks to all of you for responding to my post.
Re: What's most reliable way to save text to external file
Posted: Thu Apr 10, 2025 12:58 am
by FourthWorld
Regenerative software is designed to emulate human writing, but literally cannot understand a single word it writes.
Given the statistical probability models or uses, it often guesses correctly. And sometimes not so much.
We've addressed the difference between the URL syntax as a shortcut for open/write/close.
The last bit is the difference between reading and text and reading as binary.
Reading as text alters the data, to make it conform to the standards of the host OS. Line endings may be changed, and a couple other edge cases I can't recall offhand.
For complete fidelity with writing and reading, use the binary options.