Writing to text file

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

trags3
Posts: 418
Joined: Wed Apr 09, 2014 1:58 am
Location: Las Vegas, NV

Writing to text file

Post by trags3 » Tue Dec 20, 2022 10:48 pm

Hi All,
I've been away from LC for about a year and am now working on a project that I'm having some trouble with.
I want to write to a text file on the next line of a file. I want to make sure I don't overwrite what has already been written.
Here is the code I am using to write a single line (which is all I would write until the program is run again.)

on openCard
open file specialFolderPath("Documents/Vipw") & "pWork.txt" for write
put "This is a test" & cr into tText
write tText to file specialFolderPath("Documents/Vipw") & "pWork.txt" after
close file specialFolderPath("Documents/Vipw") & "pWork.txt"

I have tried several variations without success.
Thanks in advance for the help!
Tom

Emily-Elizabeth
Posts: 101
Joined: Mon Jan 03, 2022 7:10 pm

Re: Writing to text file

Post by Emily-Elizabeth » Tue Dec 20, 2022 11:21 pm

Try append instead of write

Code: Select all

open file specialFolderPath("Documents/Vipw") & "pWork.txt" for append

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Writing to text file

Post by dunbarx » Tue Dec 20, 2022 11:29 pm

@Trag.

Check the dictionary under "write to file". You will see two candidates that should append to the text of an existing file:

- The "at" keyword allows you to specify a certain char, if you know it.
- "EOF" is what you likely want.

Emily.

Hi.

Is there such a thing as "append" in LC? Sounds like it should be. :wink:

Craig

Emily-Elizabeth
Posts: 101
Joined: Mon Jan 03, 2022 7:10 pm

Re: Writing to text file

Post by Emily-Elizabeth » Tue Dec 20, 2022 11:32 pm

Screenshot 2022-12-20 at 17.30.55.png
Documentation shows there is an append for the open file command

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

Re: Writing to text file

Post by SparkOut » Wed Dec 21, 2022 8:38 am

For specialFolderPath use "documents" (lower case d) and only "documents" within the function, add the subfolder with the filename afterwards. (Windows file paths are not case sensitive but others eg Android are.)
As you have it, you miss out the slash between the subfolder and the filename as well, but it wouldn't work like that.
Append is the term to use here. But if the data is not too much, you could put tText after url ("file:" & specialFolderPath("documents") & "Vipw/pWork.txt") to do the whole thing in one go. (Maybe you'd need to add a CR before or after tText to keep updates from mashing onto the same line as existing dara.)

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Writing to text file

Post by Klaus » Wed Dec 21, 2022 9:59 am

There is a SLASH missing after "Vipw" (presumed that is a subfolder in "documents")!

Code: Select all

...
## open file specialFolderPath("Documents/Vipw") & "pWork.txt" for write
open file specialFolderPath("Documents/Vipw/") & "pWork.txt" for write
...

trags3
Posts: 418
Joined: Wed Apr 09, 2014 1:58 am
Location: Las Vegas, NV

Writing to text file

Post by trags3 » Wed Dec 21, 2022 11:56 am

Hello Again,
Thanks for the help!
The Append works swimmingly!
Tom

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Writing to text file

Post by Klaus » Wed Dec 21, 2022 12:40 pm

You can also use the shorter URL syntax here!

Code: Select all

...
## open file specialFolderPath("Documents/Vipw") & "pWork.txt" for write
put specialFolderPath("documents/Vipw/") & "pWork.txt" into tFile
put "This is a test" & cr into tText
put tText AFTER url("file:" & tFile)
...
Actually you can use any (text modifying) syntax like in a field:

Code: Select all

...
set itemdel to TAB
put tText after/before/into char 42 of item 3 of line 2 of url("file:" & tFile)
## etc. you get the picture...
...

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Writing to text file

Post by richmond62 » Wed Dec 21, 2022 3:01 pm

Personally I'd load the contents of the text file into a field, append the new text and export it again.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Writing to text file

Post by FourthWorld » Wed Dec 21, 2022 5:07 pm

trags3 wrote:
Wed Dec 21, 2022 11:56 am
Hello Again,
Thanks for the help!
The Append works swimmingly!
Tom
It is the best option when adding data to a file without altering existing contents.

By far.

Think of it as "Log Mode", and consider how many processes need to maintain logs.

It's one of the most optimized write options, in LC, in the OS, the file system, all the way down to the storage device controller.

Mechanically, it's one of the simplest write operations you can invoke.

Most other options require first reading the full file contents from disk, appending in memory, and then redundantly writing the same contents back in place but with the new stuff added.

Or to avoid the unnecessary read and redundant write you could get the file length, then do a seek, then write the new stuff to the specified end of the file.

But append does that for you, automating the seek so you don't have the extra seek statement passed along from your script to the engine to the OS to the device controller.

Instead, when specifying log mode by using append, the seek is safely assumed and therefore doesn't need to be interpreted. Append always seeks to the end of the file, so it doesn't need specifying, happening automatically in highly optimized object code closer to the storage device. Your one LiveCode statement just throws data at it and the OS and device controller take care of the rest for you, in the most efficient way possible.

Fun trivia: the optimized nature of append is a big part of why CouchDB is designed to use an append-only format. Aiming for high volumes of rapid storage, append-only bypasses the complex btree structures that characterize many other DB storage formats.

For logging, or any task like logging, append is your best friend.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Writing to text file

Post by dunbarx » Wed Dec 21, 2022 8:07 pm

All.

Does this mean that there is indeed an "append" parameter available to the "write to file" command? I could not find it at all.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Writing to text file

Post by dunbarx » Wed Dec 21, 2022 8:10 pm

Ah. I just saw Emily's post that "append' is indeed a parameter for the "open file" command.

OK.

Anyone have an opinion on the pros and cons of using "append" with the "open file" command as opposed to using "EOF" with the "write to file' command?

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Writing to text file

Post by FourthWorld » Wed Dec 21, 2022 8:46 pm

dunbarx wrote:
Wed Dec 21, 2022 8:10 pm
Anyone have an opinion on the pros and cons of using "append" with the "open file" command as opposed to using "EOF" with the "write to file' command?
https://forums.livecode.com/viewtopic.p ... d4#p220470
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Writing to text file

Post by dunbarx » Thu Dec 22, 2022 2:28 am

Richard, circling back, or rather upward, I still do not see precisely the answer to my question.

This may be because I rarely read or write to external files, so know really very little about it. But am I misinterpreting what "EOF" means in the "write to file" command? Doesn't that mean that any new text is appended?

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Writing to text file

Post by FourthWorld » Thu Dec 22, 2022 3:12 am

EOF can be used in different ways. Give me an example statement using that end-of-file marker and I'll do my best to describe what's happening under the hood.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Talking LiveCode”