Writing to text file
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Writing to text file
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
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
-
- Posts: 101
- Joined: Mon Jan 03, 2022 7:10 pm
Re: Writing to text file
Try append instead of write
Code: Select all
open file specialFolderPath("Documents/Vipw") & "pWork.txt" for append
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Writing to text file
@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.
Craig
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.
Craig
-
- Posts: 101
- Joined: Mon Jan 03, 2022 7:10 pm
Re: Writing to text file
Documentation shows there is an append for the open file command
Re: Writing to text file
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.)
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.)
Re: Writing to text file
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
...
Writing to text file
Hello Again,
Thanks for the help!
The Append works swimmingly!
Tom
Thanks for the help!
The Append works swimmingly!
Tom
Re: Writing to text file
You can also use the shorter URL syntax here!
Actually you can use any (text modifying) syntax like in a field:
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)
...
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...
...
-
- Livecode Opensource Backer
- Posts: 9389
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Writing to text file
Personally I'd load the contents of the text file into a field, append the new text and export it again.
-
- VIP Livecode Opensource Backer
- Posts: 9842
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Writing to text file
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Writing to text file
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Writing to text file
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9842
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Writing to text file
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Writing to text file
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9842
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Writing to text file
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn