This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.
The third edition targets Lua 5.2 and is available at Amazon and other bookstores.
By buying the book, you also help to support the Lua project.
|Programming in Lua|
|Part III. The Standard Libraries Chapter 21. The I/O Library|
For more control over I/O,
you can use the complete model.
A central concept in this model is the file handle,
which is equivalent to streams (
FILE*) in C:
It represents an open file with a current position.
To open a file, you use the
which mimics the
fopen function in C.
It receives as arguments the name of the file to open
plus a mode string.
That mode string may contain an `
r´ for reading,
w´ for writing (which also erases any previous content of the file),
or an `
a´ for appending,
plus an optional `
b´ to open binary files.
open function returns a new handle for the file.
In case of errors,
open returns nil,
plus an error message and an error number:
print(io.open("non-existent file", "r")) --> nil No such file or directory 2 print(io.open("/etc/passwd", "w")) --> nil Permission denied 13The interpretation of the error numbers is system dependent.
A typical idiom to check for errors is
local f = assert(io.open(filename, mode))If the
openfails, the error message goes as the second argument to
assert, which then shows the message.
After you open a file,
you can read from it or write to it
with the methods
They are similar to the
but you call them as methods on the file handle,
using the colon syntax.
For instance, to open a file and read it all,
you can use a chunk like this:
local f = assert(io.open(filename, "r")) local t = f:read("*all") f:close()
The I/O library also offers handles for the three predefined C streams:
So, you can send a message directly to the error stream
with a code like this:
We can mix the complete model with the simple model.
We get the current input file handle
io.input(), without arguments.
We set the current input file handle
with the call
(Similar calls are also valid for
if you want to change the current input file temporarily,
you can write something like this:
local temp = io.input() -- save current file io.input("newinput") -- open a new current file ... -- do something with new input io.input():close() -- close current file io.input(temp) -- restore previous current file
|Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved.|