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:
Open an existing file for reading. If the file does not exist,
Open or create a file for writing. If the file already exists, its current contents will be destroyed.
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:
Open an existing file for update. You can read from or write to any location in the file. If the file does not exist,
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.
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
You can both read from and write to a file opened for update. However,
before switching from one to the other, you must make an
call, to flush the file’s buffer.
fopen() fails, it returns a 0.
close() and closeall to close files opened with