file.open

Syntax

f = file.open(sname<,smode><,stype>)

Open a file for reading/writing.

smode indicates the open mode. It is a string that must match (as a command keyword) one of the following values:
  • read: Open for read only. This is the default.
  • write: Open for write only.
  • append: Open for write, but preserve the current contents of the file and start writing at the end.
  • both: Open for read and write.
  • rw: Open for read and write.
  • readwrite: Open for read and write.
stype is the type of file. The default file type is save. Currently available file types are:
  • save: The default. This is the same format used for save files and results files.
  • text: A text file, each line in the file is read as a string. On write each entry in the list is converted to a string if necessary and written as a line.
  • json: A text file structured in JSON format.
  • fish: A file in a FISH specific legacy format. Compatible with older versions, and extended to support some of the newer types.
  • binary: A file in binary format. Each entry in the list is treated as an integer from 0 to 255, and is written/read as a separate byte in the file.

The save and json formats allow all FISH types except pointers, and preserve hierarchical data structures (lists of lists of maps etc.). The text type converts all entries to text strings on write, as if the string intrinsic were used. The fish type is restricted to bool, integer, float, string, vector, and tensor types.

Returns:f - A pointer to the file opened.
Arguments:sname - file name smode - the file open mode. defaults to read. stype - the file type. defaults to save.

Usage Example

The following example illustrates how file.open can be used

model new
zone create brick size 2 2 2
fish define output_gps
    local f = file.open("gps.txt","write","text")
    local l = list
    loop foreach local g gp.list
        local a = string.build("%1,%2,%3,%4", gp.id(g), ...
                       gp.pos(g)->x, gp.pos(g)->y, gp.pos(g)->z)
        l = list.append(l,a)
    endloop
    file.write(f,l)
    file.close(f)
end
[output_gps]