Opens a file.
- filename (string) – name of file to open, can contain a path specification.
- omode (string) – file I/O mode. (See Remarks, below.)
fh (scalar) – file handle.
Carriage return-linefeed conversion for files opened in text mode is unnecessary, because in Linux/macOS a newline is simply a linefeed.
omode is a sequence of characters that specify the mode in which to open the file. The first character must be one of:
|r||Open an existing file for reading. If the file does not exist,
|w||Open or create a file for writing. If the file already exists, its current contents will be destroyed.|
|a||Open or create a file for appending. All output is appended to the end of the file.|
To this can be appended a
+ and/or a
+ indicates the file is to
opened for reading and writing, or update, as follows:
|r+||Open an existing file for update. You can read from or write to any location in the file. If the file does not exist,
|w+||Open or create a file for update. You can read from or write to any location in the file. If the file already exists, its current contents will be destroyed.|
|a+||Open or create a file for update. You can read from any location in the file, but all output will be appended to the end of the file.|
b indicates whether the file is to be opened in text or
binary mode. If the file is opened in binary mode, the contents of the
file are read verbatim; likewise, anything output to the file is written
In text mode (the default), carriage return-linefeed sequences
are converted on input to linefeeds, or newlines. Likewise on output,
newlines are converted to carriage return-linefeeds. Also in text mode,
Ctrl+Z (char 26) is encountered during a read, it is interpreted as
an end-of-file character, and reading ceases. In binary mode,
read in uninterpreted.
The order of
b is not significant;
r+b mean the same
fopen() fails, it returns a 0.