fopen - Open file, or obtain information about open files

Syntax

fid = fopen(filename)
fid = fopen(filename, permission)
fid = fopen(filename, permission_tmode)
[fid, message] = fopen(filename, permission)
[fid, message] = fopen(filename, permission, machineformat)
[fid, message] = fopen(filename, permission, machineformat, encoding)
fids = fopen('all')
[filename, permission, machineformat, encoding] = fopen(fid)

Description

fid = fopen(filename) opens the file filename for read access. (On Windows® systems, fopen opens files for binary read access.) The filename argument is a string enclosed in single quotes. It can be a MATLABPATH relative partial pathname if the file is opened for reading only. A relative path is always searched for first with respect to the current directory. If it is not found, and reading only is specified or implied, then fopen does an additional search of the MATLABPATH.

fid is a scalar MATLAB® integer, called a file identifier. You use the fid as the first argument to other file input/output routines. If fopen cannot open the file, it returns -1. Two file identifiers are automatically available and need not be opened. They are fid=1 (standard output) and fid=2 (standard error).

fid = fopen(filename, permission) opens the file filename in the specified permission. The permission argument can be any of the following:

Permission Specifiers

PermissionDescription

'r'

Open file for reading (default).

'w'

Open file, or create new file, for writing; discard existing contents, if any.

'a'

Open file, or create new file, for writing; append data to the end of the file.

'r+'

Open file for reading and writing.

'w+'

Open file, or create new file, for reading and writing; discard existing contents, if any.

'a+'

Open file, or create new file, for reading and writing; append data to the end of the file.

'A'

Append without automatic flushing; used with tape drives.

'W'

Write without automatic flushing; used with tape drives.

fid = fopen(filename, permission_tmode) on Windows systems, opens the file in text mode instead of binary mode (the default). The permission_tmode argument consists of any of the specifiers shown in the Permission Specifiers table above, followed by the letter t, for example 'rt' or 'wt+. On UNIX® systems, text and binary mode are the same. (UNIX is a registered trademark of The Open Group in the United States and other countries).

Binary and Text Modes

Mode

Behavior

Binary

No characters are given special treatment.

Text

On a read operation, whenever MATLAB encounters a carriage return followed by a newline character, it removes the carriage return from the input. On a write or append operation, MATLAB inserts a carriage return before any newline character.

[fid, message] = fopen(filename, permission) opens a file as above. If it cannot open the file, fid equals -1 and message contains a system-dependent error message. If fopen successfully opens a file, the value of message is empty.

[fid, message] = fopen(filename, permission, machineformat) opens the file with the specified permission and treats data read using fread or data written using fwrite as having a format given by machineformat. machineformat is one of the following strings:

Full Precision Support

'ieee–be' or 'b'

IEEE® floating point with big-endian byte ordering

'ieee–le' or 'l'

IEEE floating point with little-endian byte ordering

'ieee-be.l64' or 's'

IEEE floating point with big-endian byte ordering and 64-bit long data type

'ieee-le.l64' or 'a'

IEEE floating point with little-endian byte ordering and 64-bit long data type

'native' or 'n'

Numeric format of the machine on which MATLAB is running (the default)

'vaxd' or 'd'

VAX D floating point and VAX ordering

'vaxg' or 'g'

VAX G floating point and VAX ordering

Limited Precision Support: (double or equivalent)

'cray' or 'c'

Cray floating point with big-endian byte ordering

[fid, message] = fopen(filename, permission, machineformat, encoding) opens the specified file using the specified permission and machineformat. encoding is a string that specifies the character encoding scheme associated with the file. It must be the empty string ('') or a name or alias for an encoding scheme. Some examples are 'UTF-8', 'latin1', 'US-ASCII', and 'Shift_JIS'. For common names and aliases, see the Web site http://www.iana.org/assignments/character-sets. If encoding is unspecified or is the empty string (''), MATLAB's default encoding scheme is used.

fids = fopen('all') returns a row vector containing the file identifiers of all open files, not including 1 and 2 (standard output and standard error). The number of elements in the vector is equal to the number of open files.

[filename, permission, machineformat, encoding] = fopen(fid) returns the filename, permission, machineformat, and encoding values used by MATLAB when it opened the file associated with identifier fid. MATLAB does not determine these output values by reading information from the opened file. For any of these parameters that were not specified when the file was opened, MATLAB returns its default value. The encoding string is a standard character encoding scheme name that may not be the same as the encoding argument used in the call to fopen that opened the file. An invalid fid returns empty strings for all output arguments.

The 'W' and 'A' modes do not automatically perform a flush of the current output buffer after output operations.

Examples

The example uses fopen to open a file and then passes the fid returned by fopen to other file I/O functions to read data from the file and then close the file.

fid = fopen('fgetl.m');
while 1
    tline = fgetl(fid);
    if ~ischar(tline),   break,   end
    disp(tline)
end
fclose(fid);

See Also

fclose, ferror, fprintf, fread, fscanf, fseek, ftell, fwrite

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS