Opens a file and returns a file handle for working with it.
Modes can be a string which is:
- r - read mode
- w - write mode (overwrites existing)
- a - append mode (appends to existing)
- b - binary mode
- r+ - update mode (existing data preserved)
- w+ - update mode (existing data erased)
- a+ - append update mode (existing data preserved, append at end of file only)
If the file cannot be opened this function does not raise an error (unlike io.input and io.output) but returns 3 things:
- nil
- An error message (string)
- An error code (number)
Thus a sensible thing to do is wrap the io.open call with an assert, as in the example:
f = assert (io.open ("test.txt", "r")) -- open it
s = f:read ("*a") -- read all of it
print (s) -- print out
f:close () -- close it