file-open 2.0
file-open string
This command will interpret string as a path name to a file
and open the file. You may then use the reporters file-read, file-read-line, and file-read-characters to read in from
the file, or file-write, file-print, file-type,
or file-show to write out to the file.
Note that you can only open a file for reading or writing but not
both. The next file i/o primitive you use after this command
dictates which mode the file is opened in. To switch modes, you
need to close the file using file-close.
Also, the file must already exist if opening a file in reading mode.
When opening a file in writing mode, all new data will be appended
to the end of the original file. If there is no original file, a
new blank file will be created in its place. (You must have write
permission in the file’s directory.) (If you don’t want to
append, but want to replace the file’s existing contents, use file-delete to delete it first, perhaps
inside a carefully if you’re not sure
whether it already exists.)
Note that the string can either be a file name or an absolute file
path. If it is a file name, it looks in whatever the current
directory is. This can be changed using the command set-current-directory. It is defaulted
to the model’s directory.
file-open "my-file-in.txt"
print file-read-line
=> First line in file ;; File is in reading mode
file-open "C:\NetLogo\my-file-out.txt"
;; assuming Windows machine
file-print "Hello World" ;; File is in writing modeOpening a file does not close previously opened files. You can use file-open to switch back and forth between multiple open
files.
See also file-close See also file-close-all.