Page 1 of 1

Writing text to a new line

Posted: Thu Jan 23, 2014 4:43 pm
by altamative131
I am trying to export data and add to an existing text file. I do not know how to get Livecode to start on the next line, instead of replacing the current text in the file.

Basically here is what I am doing. I have Info.txt that has data in it already, I am just trying to add to it in after the existing info.

Any help would be much appreciated.

Thanks

Re: Writing text to a new line

Posted: Thu Jan 23, 2014 4:48 pm
by Klaus
Hi altamative131,

1. welcome to the forum! :D

2. You can use FILES must like Livecode fields!
Like this:
...
put specialfolderpath("desktop") & "/Info.txt" into tFile
## TText conatins any text you want to append:
## Write NEW LINE and the text AFTER the file:
put CR & tTExt AFTER url("file:" & tFile)
...
## To insert BEFORE any other text
...
put tText & CR BEFORE url("file:" & tFile)
...
## Even this:
put tText into word 3 of line 4 of url("file:" & tFile)
...
You get the picture :D


Best

Klaus

Re: Writing text to a new line

Posted: Thu Jan 23, 2014 4:50 pm
by altamative131
Yes, holy geez I was over thinking that entire thing. Thank you

Re: Writing text to a new line

Posted: Thu Jan 23, 2014 5:06 pm
by dunbarx
Another method, and I post it here just so you get a clearer picture of how LC handles what it calls "chunking", would be something like:

put newText into line (the number of lines of oldText +1) of oldtext

So if oldText had 30 lines, the newText would start at line 31. No need for CR thinking.

This has benefits in certain cases.

Craig Newman

Re: Writing text to a new line

Posted: Thu Jan 23, 2014 5:42 pm
by Klaus
altamative131 wrote:Yes, holy geez I was over thinking that entire thing. Thank you
You'll surely have this feeling some more often in the future! :D

Think simple = Livecode!

Re: Writing text to a new line

Posted: Thu Jan 23, 2014 5:45 pm
by FourthWorld
LiveCode's "open file" command allows many options, including "update", "read", "write", and "append". The "append" option will automatically write to the end of the file.

In addition to being convenient, it's also very efficient for that specific task since it doesn't need to also read the file contents. For small files the performance gain won't matter much, but with large files it can make a noticeable difference.

See the Dictionary entry for the "open file" command for details.