Does anyone know where can I find examples on file IO? I did search on the net and failed to find any relevant examples. All I found was the individual function call usage but not as a collective use. I tried several ways to log some data to a file but nothing seems to be written out. The following is one of the ways that I tried. I will really appreciate if someone can help to point out where is my mistake. Thank you very much.
The only problem with the code sample below is in the first uncommented line; you need to use “into” not “to”
//filename is log.rtf @ root
//put “/log.rtf” to filename – this is incorrect
put “/log.rtf” into filename – should be this
open file filename for appending
write “testing” to file filename
close file filename
Note that by default, the working directory is the root of the filesystem ("/") and that regardless of that, naming your file “/log.rtf” will create it iin the root of the filesystem. You can view and change the current working directory with the following commands:
put the directory -- displays the current working directory set the directory to "~/Documents" -- set the working directory
We could supply some examples, but this is pretty much the gyst of it. If you have any other specific questions about file IO, please post them and we’ll get you some more answers and examples.
Thanks for the reply. But I realise the “into” was my typo and not because I use “in” in my actual script. So, can I say that my script should work fine? My log.rtf was empty when I opened the file… please help. Thank you once again.
Yes your code segment should be working fine, the most likely problem is that you are naming the file .rtf
An RTF file requires certain formatting to be valid so if you try to open the file created by this script in TextEdit it will not be able to do so.
Try naming the file log.txt and then you can see that the file is written as you would expect.
You are right. Thanks a great deal