fopen

Purpose

Open a file or obtain information about open files.

Synopsis

fid = fopen('filename')
fid = fopen('filename', 'permission')
[fid, message] = fopen('filename','permission', 'architecture')
fids = fopen('all')
[filename, permission, architecture] = fopen(fid)

Description

fid = fopen('filename') opens the file in filename and returns fid, the file identifier.

fopen('filename','permission') opens the file filename in the mode specified by permission. Legal file permission strings are

-------------------------------------------------------------
'r'   Open the file for reading (default).                      
'r+'  Open the file for reading and writing.                    
'w'   Delete the contents of an existing file or create a new   
      file, and open it for writing.                            
'w+'  Delete the contents of an existing file or create new     
      file, and open it for reading and writing.                
'a'   Create and open a new file or open an existing file for   
      writing, appending to the end of the file.                
'a+'  Create and open new file or open an existing file for     
      reading and writing, appending to the end of the file.    
-------------------------------------------------------------
You can also add a 'b' to these strings, for example, 'rb', on systems that distinguish between text and binary files. Under DOS and VMS, for example, you cannot read a binary file unless you set the permission to 'rb'.

If fopen successfully opens the file, it returns a file identifier fid, which is an integer greater than two, and the value of message is empty. The fid is used with other file I/O routines to identify the file on which to perform the operations.

Three fids are predefined: 0 corresponds to standard input, which is always open for reading (permission set to 'r'), 1 corresponds to standard output, which is always open for appending (permission set to 'a'), and 2 corresponds to standard error, which is always open for appending (permission set to 'a'). Standard input, output and error cannot be explicitly opened or closed.

If fopen does not successfully open the file, it returns a -1 value for fid. In that case, the value of message is a string that can help you determine the type of error that occurred.

[fid, message] = fopen('filename','permission', 'architecture') defines the numeric format of the file, architecture, allowing you to share files between machines of different architectures. The argument can be one of these strings:

---------------------------------------------------------------
'native' or 'n'      The numeric format of the machine you are   
                     currently running                           
'ISIEEE-LE' or 'l'   IEEE Little Endian formats                  
'ISIEEE-BE' or 'b'   IEEE Big Endian formats                     
'vaxdv' or 'd'       VAX D-float format                          
'vaxg' or 'gv'       VAX G-float format                          
'crayv' or 'c'       Cray numeric format                         
---------------------------------------------------------------
You can omit the 'architecture' argument. If you do, the numeric format of the local machine is used. Individual calls to fread or fwrite can override the numeric format specified in a call to fopen.

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

[filename, permission, architecture] = fopen(fid) returns the filename string, the permission string, and the architecture string associated with the specified file. An invalid fid returns empty strings for all output arguments. Both permission and architecture are optional.

See Also

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

(c) Copyright 1994 by The MathWorks, Inc.