Navigation

Operators and Keywords

Function List:

C++ API

: fid = fopen (name)
: fid = fopen (name, mode)
: fid = fopen (name, mode, arch)
: [fid, msg] = fopen (…)
: fid_list = fopen ("all")
: [file, mode, arch] = fopen (fid)

Open a file for low-level I/O or query open files and file descriptors.

The first form of the fopen function opens the named file with the specified mode (read-write, read-only, etc.) and architecture interpretation (IEEE big endian, IEEE little endian, etc.), and returns an integer value that may be used to refer to the file later. If an error occurs, fid is set to -1 and msg contains the corresponding system error message. The mode is a one or two character string that specifies whether the file is to be opened for reading, writing, or both.

The second form of the fopen function returns a vector of file ids corresponding to all the currently open files, excluding the stdin, stdout, and stderr streams.

The third form of the fopen function returns information about the open file given its file id.

For example,

myfile = fopen ("splat.dat", "r", "ieee-le");

opens the file splat.dat for reading. If necessary, binary numeric values will be read assuming they are stored in IEEE format with the least significant bit first, and then converted to the native representation.

Opening a file that is already open simply opens it again and returns a separate file id. It is not an error to open a file several times, though writing to the same file through several different file ids may produce unexpected results.

The possible values ‘mode’ may have are

r’ (default)

Open a file for reading.

w

Open a file for writing. The previous contents are discarded.

a

Open or create a file for writing at the end of the file.

r+

Open an existing file for reading and writing.

w+

Open a file for reading or writing. The previous contents are discarded.

a+

Open or create a file for reading or writing at the end of the file.

Append a "t" to the mode string to open the file in text mode or a "b" to open in binary mode. On Windows and Macintosh systems, text mode reading and writing automatically converts linefeeds to the appropriate line end character for the system (carriage-return linefeed on Windows, carriage-return on Macintosh). The default when no mode is specified is binary mode.

Additionally, you may append a "z" to the mode string to open a gzipped file for reading or writing. For this to be successful, you must also open the file in binary mode.

The parameter arch is a string specifying the default data format for the file. Valid values for arch are:

"native" or "n" (default)

The format of the current machine.

"ieee-be" or "b"

IEEE big endian format.

"ieee-le" or "l"

IEEE little endian format.

However, conversions are currently only supported for ‘native’, ‘ieee-be’, and ‘ieee-le’ formats.

When opening a new file that does not yet exist, permissions will be set to 0666 - umask.

See also: fclose, fgets, fgetl, fscanf, fread, fputs, fdisp, fprintf, fwrite, fskipl, fseek, frewind, ftell, feof, ferror, fclear, fflush, freport, umask.

Package: octave