Correct syntax fpr appending data

J. Landman Gay jacque at hyperactivesw.com
Mon Mar 6 14:52:12 EST 2006


Ben Bock wrote:
> What is the correct syntax for Rev to append data to the next line in
> a text file?
<snip<
> 
> put fld "data field " into url "file:test data.txt"
> 
> get url "file:test data.txt"
> 
> put it into fld "sample field that proves to me something is going to
> the test data file"
> 
> save url "file:test data.txt"

You don't need the "save" line -- putting data into a URL automatically 
opens the file, writes to it, closes and saves it.

You can re-read the text file, add more info after the existing info, 
and the put the data back into the URL, as you are doing now. "Putting" 
into a URL is definitely one of the easiest methods of saving text 
files, because it is a one-liner.

But in some cases it is worth a look at the original way to save files 
to disk. This uses the "open file", "write to file", and "close file" 
syntax. These are much more powerful than the automated "put into URL" 
syntax.

You can open a file and leave it open if you need to, for one thing, 
only closing it at the end of the session. This is good for repeated 
read/write operations. And more advantageous to your situation, you can 
open a file in various modes depending on what you want to do. You can 
open a file as read-only, or as write-only, or -- and this is what you 
want -- as "append". If you open a file for appending, anything you 
write automatically gets placed at the end without overwriting anything 
else in there. That describes your situation exactly.

So, you could do this:

   open file "test data.txt" for append
   write field "data field" to file "test data.txt"
   close file "test data.txt"

See the "open file" and "write to file" entries in the docs for lots of 
ideas. It is a more powerful way to use disk files.

-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com



More information about the Use-livecode mailing list