July 2018
Beginner
202 pages
5h 42m
English
When writing to a file or reading from a file, that file needs to be opened first. Lua provides the io.open function to open files. On success, the io.open function will return a file handle. On failure, it will return nil:
file = io.open("my_file.txt"); -- Opens existing file in read only mode
The preceding line of code will open a file in read-only mode. What if you want to write to a file? The io.open function takes an optional second argument, which is a string. This optional second argument controls the mode in which the file will be opened. Valid values are as follows: