open
open(path[, access])
This function opens the file specified by path and returns an identifier that may be used in subsequent calls to getline, put, close, and other I/O functions. The path name is expanded using expand_file_name to resolve directory references and environment variables.
The access argument determines how the file is to be accessed. It must have one of the following values:
• r — open for reading only. The file must already exist.
• w — open for writing only. The file is created if it does not exist or truncated if it does.
• a — open for writing only. The file is created if it does not exist; if it does, then the pointer is positioned so that new output is added to the end of the file.
• r+ — open for reading and writing. The file must already exist.
• w+ — open for reading and writing. The file is created if it does not exist or truncated if it does.
• a+ —open for reading and writing. The file must already exist and the pointer is positioned so that new output is added to the end of the file.
In addition, the following character may be appended to any of the access strings:
• b — open in binary mode. This is required to read/write binary files using the read and write functions. If b is not specified, then characters read from a file will be converted from the system character set to Unicode and vice-versa on output.
The default for access is r.
If a file is opened for both reading and writing, then a seek must be used between a read and a write operation.
There is a limit of, at most, 20 open file identifiers. The number may be less depending on the operating system.
If the file cannot be opened, open returns -1.
The open function also supports the special path names allowed for the output= option of the eval and show commands. If the first character of the output file starts with a question mark ?, then the file name is actually a variable name whose value is set to the output written to the file identifier. If the second character is >, then the output is appended to the current value of the variable instead of replacing it. For example,
fid = open("?OUTPUT", "w")
returns a file identifier that can be used with put to write to a variable. The open function also supports the "*" (asterisk) output specification to write to the message window, thus
fid = open("*", "w")
returns a file identifier that can be used with put to send output to the message window. These extensions allow a generic output function to be written that can write to a file, variable or message window.
Related Topics