Page 1 of 1

[Solved] Writing data to text file

Posted: Mon May 20, 2019 8:03 pm
by redfield
Hi guys,
I found two different ways of writing data into text files. Both methods work for me, so I am wondering what may be the difference between them? Any advantages or disadvantages?

1.

Code: Select all

put specialfolderpath("Home") & "/test" into theFile
open file theFile for write 
write "hello" to file theFile
close file theFile

2.

Code: Select all

put "file:" & specialfolderpath("Home") & "/test" into theFile
put "hello" into line 1 of URL(theFile)

Re: Writing data to text file

Posted: Mon May 20, 2019 8:13 pm
by Klaus
The url syntax is shorter! :D

1. The "open file..." syntax is better suited when dealing with LARGE files like server log file with hundreds of MB.

2. The url syntax will read the complete content of the file into RAM do what you want it to and then write it back to disk. Since nowadays we ususally have LOTS of ram installed on our machines, most of the time this will work fine.

Re: Writing data to text file

Posted: Mon May 20, 2019 8:14 pm
by FourthWorld
To write a file in one pass, the URL is method is simpler to write. If you need to do multiple writes, seeks, or other operations beyond a one-pass write, the "open file" and "close file" commands are what you'd use for that flexibility.

Re: Writing data to text file

Posted: Mon May 20, 2019 9:44 pm
by Klaus
FourthWorld wrote:
Mon May 20, 2019 8:14 pm
To write a file in one pass, the URL is method is simpler to write. If you need to do multiple writes, seeks, or other operations beyond a one-pass write, the "open file" and "close file" commands are what you'd use for that flexibility.
Yes, and that. :-)