July 2018
Beginner
202 pages
5h 42m
English
Writing data to a file is done with the write function of the file handle. This function will write whatever arguments are passed to it into the file. The following code creates a data.txt file, if one does not exist, or replaces the contents of the existing file:
file = io.open("data.txt", "w")file:write("foo")file:write("bar")
After running the previous code, a data.txt file would contain the foobar string. Looking at the code, it's reasonable to expect "foo" and "bar" to be on separate lines. By default, the write function does not add any newline characters. You have to add line breaks manually, like so:
file = io.open("data.txt", "w")file:write("foo", "\n") -- file:write("foo\n") -- would also workfile:write("bar")
The ...