file — Read and write ASCII text and binary files 3
When a file is opened read, the file must already exist, or an error message will be issued. The
file is positioned at the top (tof), so the first file read reads at the beginning of the file. Both
local files and files over the net may be opened for read.
When a file is opened write and the replace or append option is not specified, the file must
not exist, or an error message will be issued. The file is positioned at the top (tof), so the first
file write writes at the beginning of the file. Net files may not be opened for write.
When a file is opened write and the replace option is also specified, it does not matter whether
the file already exists; the existing file, if any, is erased beforehand.
When a file is opened write and the append option is also specified, it also does not matter
whether the file already exists; the file will be reopened or created if necessary. The file will be
positioned at the append point, meaning that if the file existed, the first file write will write at
the first byte past the end of the previous file; if there was no previous file, file write begins
writing at the first byte in the file. file seek may not be used with write append files.
When a file is opened read write, it also does not matter whether the file exists. If the file
exists, it is reopened. If the file does not exist, a new file is created. Regardless, the file will be
positioned at the top of the file. You can use file seek to seek to the end of the file or wherever
else you desire. Net files may not be opened for read write.
Before opening a file, you can determine whether it exists by using confirm file; see [P] confirm.
text and binary determine how the file is to be treated once it is opened. text, the default, means
ASCII text files. In ASCII text, files are assumed to be composed of lines of characters, with
each line ending in a line-end character. The character varies across operating systems, being line
feed under Unix, carriage return under Mac, and carriage return/line feed under Windows. file
understands all the ways that lines might end when reading and assumes that lines are to end in
the usual way for the computer being used when writing.
The alternative to text is binary, meaning that the file is to be viewed merely as a stream of
bytes. In binary files, there is an issue of byte order; consider the number 1 written as a 2-byte
integer. On some computers (called hilo), it is written as “00 01”, and on other computers (called
lohi), it is written as “01 00” (with the least significant byte written first). There are similar issues
for 4-byte integers, 4-byte floats, and 8-byte floats.
file assumes that the bytes are ordered in the way natural to the computer being used. file
set can be used to vary this assumption. file set can be issued immediately after file open,
or later, or repeatedly.
replace and append are allowed only when the file is opened for write (which does not include
read write). They determine what is to be done if the file already exists. The default is to issue
an error message and not open the file. See the description of the options read, write, and read
write above for more details.
all is allowed when the file is opened for write or for read write. It specifies that, if the file
needs to be created, the permissions on the file are to be set so that it is readable by everybody.
ASCII text output specifications
"string" and ‘"string"’ write string into the file, without the surrounding quotes.
(exp) evaluates the expression exp and writes the result into the file. If the result is numeric, it is
written with a %10.0g format, but with leading and trailing spaces removed. If exp evaluates to a
string, the resulting string is written, with no extra leading or trailing blanks.