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

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Writing to text file

Post by stam » Fri Dec 23, 2022 3:00 am

FourthWorld wrote:
Thu Dec 22, 2022 9:11 pm
But there's one subtle distinction that turns out to make it an especially good choice for some log files. This wasn't easy to find; most online discussions of append only describe how to use it, not why it exists. But Linux System Programming by Robert Love, a favorite in my bookshelf, offers the only why explanation I could put my hands on:
Append Mode
When fd is opened in append mode (via O_APPEND), writes do not occur at the file descriptor’s current file position. Instead, they occur at the current end of the file. For example, assume that two processes intend to write to the end of the same file. This is common: consider a log of events shared among many processes. At the start, their file positions are correctly set to the end of the file. The first process writes to the end
of the file. Without append mode, once the second process attempts the same, it will end up writing not to the end of the file, but to the offset that was the end of the file, before the data that the first process wrote. This means that multiple processes can never append to the same file without explicit synchronization between them because they will encounter race conditions. Append mode avoids this problem. It ensures that the file position is always set to the end of the file so all writes always append, even when there are multiple writers. You can think of it as an atomic update to the file position preceding each write request. The file position is then updated to point at the end of the newly written data. This will not matter to the next call to write(), as it updates the file position automatically, but it might matter if you next call read() for some odd reason.
Thanks Richard that is very helpful - I was always planning on using 'append' but this makes more sense now....

richmond62 wrote:
Thu Dec 22, 2022 11:10 pm
So, while you may characterise my looking up 'append' and nothing further as 'lazy' I would choose to lay the blame
firmly at the door of the dictionary (and, by extension, its authors) who, frankly, did a sloppy job with 'append'.
Not sure I agree with that. The dictionary definition is:
open file <filePath> [for [ {[<encoding>] text | binary}] {update | read | write | append}]
They give several examples, all of which show filepath, not URL:

Code: Select all

open file "temp.txt"
open file "/etc/Glossary" for write
open file (myFilePath & "/" & myFileName) for binary read
Even if ignoring the documentation, autocomplete specifies filepath, not URL:

Code: Select all

open file filePath for append
I really don't think the blame can be laid with the documentation authors... I'd just chalk this up to oversight...

S.

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 » Fri Dec 23, 2022 9:05 am

Even if ignoring the documentation, autocomplete specifies filepath, not URL
Possibly; but I never turn on autocomplete, as I nivir tern onn spelchik. :?

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 » Fri Dec 23, 2022 9:14 am

Code: Select all

on mouseUp
   set the ink of me to srcCopy
   answer file "Choose a text file to add extra text at the end"
   if the result = "cancel" 
   then exit mouseUp
   else
      put the longFilePath of it into WEG
      open file WEG for append
      put (cr & fld "fTEXT") into nTEXT
      write nTEXT to file WEG
      close file WEG
end if
end mouseUp
-
Screen Shot 2022-12-23 at 10.16.01 am.png
-
Works, but that is probably NOT a big surprise to people round these parts. 8)

Where I 'went wrong' is that instead of writing:

Code: Select all

write nTEXT to file WEG
I wrote:

Code: Select all

append nTEXT to file WEG
and, if one pauses to think that is not as silly as it may look to those who have been merrily
appending text to documents for years.

AND this is where the documentation fails, insofar as it assumes that everyone who wishes to append some text
KNOWS that they should use write . . .
I'd just chalk this up to oversight
I would just chalk this up to the fact that, all too often, all of us who have the odd dollop of programming
experience assume that the end-user who reads whatever we write about programming already knows
90% of what we know . . .

AND, whether you want to be polite and use the term 'oversight', or be slightly coarser (like me) and call it
something else, the end result is much the same.

IF LiveCode wishes to position itself as an Utter Newby all the way to Big Cheese sort of programming setup
its documentation has to be readily accessible (i.e. the Utter Newby has to be fed a few things so s/he has some sort of idea where to start,
and, subsequently what and where to look for info inwith the documentation) and comprehensive to the point of, probably,
complete and utter bloat. So, ideally, it should be rewritten from the ground up by an expert in instructional design having her hand held by
an expert in LC.

Now, at the risk of being accused of navel-gazing:

As an ESL teacher I have always had the same problem with students and pupils (and quite a few of them native speakers)
insofar as they forget that when they write instructions they should assume that the reader is a COMPLETE ignoramus and
write things tediously explicitly.

If you don't believe me fly over here to Bulgaria and attempt to teach a load of (intelligent) 9 year olds how to
make a button to move from one card to another: and, before you come, don't forget to pack your favourite
headache pills, and remember to leave your assumptions about instructing people in the fridge at home. :D

Favourite question (comes round every year like cuckoos in Spring):

"Why did you put the number in BN just now when you put it in TX yesterday, and where has TX gone?"

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 » Fri Dec 23, 2022 10:23 am

richmond62 wrote:
Fri Dec 23, 2022 9:14 am
So, ideally, it should be rewritten from the ground up by an expert in instructional design having her hand held by
an expert in LC.
Ideally, perhaps.

Affordably, they could make their investment-to-date more accessible with a universal search tool that allows free-form queries to find all relevant materials, smartly ranked, whether the answer is in the Dictionary, User Guide, issued example tutorials, online Lessons, or anything else.

People have become accustomed to search. What they're not accustomed to is knowing which LC resource collection they should be searching.

A universal search tool would take care of that for them.

And because they have a wealth of discussion around learning cataloged in the archives of the list and these forums, creating conceptually relevant tags that are not in the language but reflect how people use the language could be achieved through automated means.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Writing to text file

Post by trags3 » Thu Dec 29, 2022 5:43 pm

I have no idea what I did, but I have lost what worked to write to a file using append.

Here is the code I am using:

put fld"aDate" of this cd & tab & fld"Denom" of grp"5Dollars" & tab & fld"LotNumber" of grp"5Dollars" & tab & fld"ItemCode" of grp"5Dollars" & tab & fld"expDate" of grp"5Dollars" & tab & fld"JDate" of grp"5Dollars" & tab & fld"Qty" of grp"5Dollars" & cr into tNewData
answer tNewData -- I put this in just to check that the data had been properly put into tNerData
open file specialFolderPath("documents/Vipw/") & "VIP.txt" for append
put specialFolderPath("documents/Vipw/") & "VIP.txt" into tFile
put tNewData into URL("file:" & tFile)

Nothing gets written to the VIP.txt file.

I know this is easy but.....

Tom

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

Re: Writing to text file

Post by Klaus » Thu Dec 29, 2022 5:55 pm

Hi Tom,

do NOT mix the OPEN FILE... and the URL syntax!
And use some SPACES, which make the whole thing better readable

Code: Select all

...
## Hint: ...of this cd is not neccessary!
put fld "aDate" & tab & fld "Denom" of grp "5Dollars" & tab & fld "LotNumber" of grp "5Dollars" & tab & fld "ItemCode" of grp "5Dollars" & \
            tab & fld "expDate" of grp "5Dollars" & tab & fld "JDate" of grp "5Dollars" & tab & fld "Qty" of grp "5Dollars" & cr into tNewData 
## open file specialFolderPath("documents/Vipw/") & "VIP.txt" for append
## put specialFolderPath("documents/Vipw/") & "VIP.txt" into tFile
## Subfolders do not work with SPECIALFOLDERPATH() codes!
put specialFolderPath("documents") & "/Vipw/VIP.txt") into tFile
put tNewData AFTER URL("file:" & tFile) 
...
Best

Klaus

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Writing to text file

Post by stam » Thu Dec 29, 2022 6:33 pm

I think you're mixing two different methods here:

if you open file for append, you write to file. Don't 'put'. If you want to use put, then don't use open file for append, use what Klaus says...

Code: Select all

put fld"aDate" of this cd & tab & fld"Denom" of grp"5Dollars" & tab & fld"LotNumber" of grp"5Dollars" & tab & fld"ItemCode" of grp"5Dollars" & tab & fld"expDate" of grp"5Dollars" & tab & fld"JDate" of grp"5Dollars" & tab & fld"Qty" of grp"5Dollars" & cr into tNewData 
put specialFolderPath("documents") & "/Vipw/VIP.txt" into tFile // i prefer my specialFolderPaths 'clean'

open tFile for append 
write tNewData to file tFile
close file tFile
Untested, but I believe this to be correct ;)
S.
Last edited by stam on Thu Dec 29, 2022 6:36 pm, edited 1 time in total.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7239
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Writing to text file

Post by jacque » Thu Dec 29, 2022 6:36 pm

Klaus' method works. Alternatively, if you want to use "open file for append" you can do this:

Code: Select all

put fld "aDate" & tab & fld "Denom" of grp "5Dollars" & tab & fld "LotNumber" of grp "5Dollars" & tab & fld "ItemCode" of grp "5Dollars" & \
            tab & fld "expDate" of grp "5Dollars" & tab & fld "JDate" of grp "5Dollars" & tab & fld "Qty" of grp "5Dollars" & cr into tNewData 
put specialFolderPath("documents") & "/Vipw/VIP.txt") into tFile
open file tFile for apoend
write tNewData to file tFile
close file tFile
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Writing to text file

Post by SparkOut » Thu Dec 29, 2022 11:33 pm

Please check what people have been showing you above, and know that specialFolderPath() is a function that resolves a path from a reference to a special folder and returns that.
That is, in this case, specialFolderPath ("documents") and not some concatenated string with a subfolder.
By all means, concatenate a subfolder name and filename to create your path, but don't mix the function with the concatenation.

Post Reply

Return to “Talking LiveCode”