|Supported File Modes
Opens a file so it can be read or written. mode consists of a first character which is one of the following:
Any of the three may be optionally followed by "b" to open the file for binary access instead of the default text access (eg, "wb" opens a file for binary output).
Text mode file handles assume UTF-8 encoding for both input and output purposes, and supports all characters within the ISO 8859-1 codepage (plus a few extras). Prior to ComputerCraft 1.76, instead only printable ASCII characters are supported. In either case, any characters not supported are translated to 0x3F (representing a question mark).
When you have opened a file you must always close the file handle, or else data may not be saved.Append mode and write mode both create a new file if none already exists (and write mode creates a new one even if one does); however, under a few builds of ComputerCraft in the 1.5x range, append mode will fail if an existing file cannot be found.
|Syntax||fs.open(string path, string mode)|
|Returns||table the file handle, or nil on error (e.g. if mode was "r" and the file did not exist, or if mode was "w" or "a" and the file was in a read-only location)|
|Creates the file "abcd" for writing, and holds a file handle to it|
local h = fs.open("abcd", "w")
|Opens "abcd" in either append or write mode, depending on whether it already exists or not.|
local h = fs.open("abcd", fs.exists("abcd") and "a" or "w")
A file handle allows access to a file. A file handle is a table; the functions within the table are accessed with the dot operator (not the colon operator, as may be more intuitive!). The examples below assume a file has already been opened and the handle stored in the variable h.
Closing a file handle
When you open a file you must remember to close the handle when you've finished with it! The write modes supported by ComputerCraft may not actually output data until this is done. A file opened in any mode exposes the following close function:
|Closes the file handle, after which it can no longer be used|
Files opened in text read mode
A file opened in mode "r" (text read mode) exposes the following functions.
|Reads the next line from the file|
|Returns||string the next line read from the file, with the end-of-line character stripped; or nil if there are no more lines in the file|
|Reads the all the text in the file|
|Returns||string the entire rest of the file, with the end-of-line character on the very last line (if present) stripped|
Files opened in text write/append mode
A file opened in mode "w" (text write mode) or "a" (text append mode) exposes the following functions:
|Writes a string of characters to the file exactly as they appear in the string data|
|Writes a string of characters to the file, then appends an end-of-line character|
if you just want to save a file, without closing the handle, then you could do a flush. ( this is normally used for logging etc...)
|Flushes the data to the specified file. (keeps the handle available afterwards)|
Files opened in binary read mode
A file opened in mode "rb" (binary read mode) exposes the following functions:
|Reads a single byte from the file and returns it|
|Returns||number the byte read from the file, or nil if there are no more bytes|
Files opened in binary write/append mode
A file opened in mode "wb" (binary write mode) or "ab" (binary append mode) exposes the following functions:
|Writes a single byte into the file|