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

Opening a file

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:

  • "r": Read-only mode will let you read the file, but not write to it. This is the default mode for opening the file. If the file does not exist, nil is ...
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