Skip to Content
Lua Quick Start Guide
book

Lua Quick Start Guide

by Gabor Szauer
July 2018
Beginner
202 pages
5h 42m
English
Packt Publishing
Content preview from Lua Quick Start Guide

Writing data

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Lua Programming

Beginning Lua Programming

Kurt Jung, Aaron Brown
Vim Masterclass

Vim Masterclass

Jason Cannon

Publisher Resources

ISBN: 9781789343229Supplemental Content