Io.read: Difference between revisions

From GiderosMobile
No edit summary
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 5: Line 5:
=== Description ===
=== Description ===
Reading from default input, basically equivalent to io.input():read.
Reading from default input, basically equivalent to io.input():read.
<source lang="lua">
<syntaxhighlight lang="lua">
(string) = io.read(format1,...)
(string) = io.read(format1,...)
</source>
</syntaxhighlight>


=== Parameters ===
=== Parameters ===
'''format1''': (string) reading format<br/>
'''format1''': (string) reading format, possible values:
*''"*all"'' reads the whole file
*''"*line"'' reads the next line, '''default'''
*''"*number"'' reads a number
*''num'' reads a string with up to num characters
'''...''': (string) more optional formats '''optional'''<br/>
'''...''': (string) more optional formats '''optional'''<br/>


=== Return values ===
=== Return values ===
'''Returns''' (string) returns read values or nil<br/>
'''Returns''' (string) returns read values or nil<br/>
=== Examples ===
<syntaxhighlight lang="lua">
local fd = io.open(path.."/"..file, "rb")
local header = fd:read(27) -- reads a string with up to 27 characters
</syntaxhighlight>
A Small Performance Trick ('''https://www.lua.org/pil/21.2.1.html''')
Usually, in Lua, it is much faster to read a file as a whole than to read it line by line. However, sometimes we must face some big files (say, tens or hundreds megabytes) for which it is not reasonable to read them all at once. If you want to handle such big files with maximum performance, the fastest way is to read them in reasonably large chunks (e.g., 8 KB each). To avoid the problem of breaking lines in the middle, you simply ask to read a chunk plus a line:
<syntaxhighlight lang="lua">
local lines, rest = f:read(BUFSIZE, "*line")
</syntaxhighlight>
The variable ''rest'' will get the rest of any line broken by the chunk. We then concatenate the chunk and this rest of line. That way, the resulting chunk will always break at line boundaries.
A typical example of that technique is this implementation of wc, a program to count the number of characters, words, and lines in a file:
<syntaxhighlight lang="lua">
local BUFSIZE = 2^13 -- 8K
local f = io.input(arg[1]) -- open input file
local cc, lc, wc = 0, 0, 0 -- char, line, and word counts
while true do
local lines, rest = f:read(BUFSIZE, "*line")
if not lines then break end
if rest then lines = lines .. rest .. '\n' end
cc = cc + string.len(lines)
-- count words in the chunk
local _, t = string.gsub(lines, "%S+", "")
wc = wc + t
-- count newlines in the chunk
_, t = string.gsub(lines, "\n", "\n")
lc = lc + t
end
print(lc, wc, cc)
</syntaxhighlight>


{{Io}}
{{Io}}

Latest revision as of 12:49, 20 August 2026

Available since: Gideros 2011.6
Class: io

Description

Reading from default input, basically equivalent to io.input():read.

(string) = io.read(format1,...)

Parameters

format1: (string) reading format, possible values:

  • "*all" reads the whole file
  • "*line" reads the next line, default
  • "*number" reads a number
  • num reads a string with up to num characters

...: (string) more optional formats optional

Return values

Returns (string) returns read values or nil

Examples

local fd = io.open(path.."/"..file, "rb")
local header = fd:read(27) -- reads a string with up to 27 characters

A Small Performance Trick (https://www.lua.org/pil/21.2.1.html)

Usually, in Lua, it is much faster to read a file as a whole than to read it line by line. However, sometimes we must face some big files (say, tens or hundreds megabytes) for which it is not reasonable to read them all at once. If you want to handle such big files with maximum performance, the fastest way is to read them in reasonably large chunks (e.g., 8 KB each). To avoid the problem of breaking lines in the middle, you simply ask to read a chunk plus a line:

local lines, rest = f:read(BUFSIZE, "*line")

The variable rest will get the rest of any line broken by the chunk. We then concatenate the chunk and this rest of line. That way, the resulting chunk will always break at line boundaries.

A typical example of that technique is this implementation of wc, a program to count the number of characters, words, and lines in a file:

local BUFSIZE = 2^13 -- 8K
local f = io.input(arg[1]) -- open input file
local cc, lc, wc = 0, 0, 0 -- char, line, and word counts
while true do
	local lines, rest = f:read(BUFSIZE, "*line")
	if not lines then break end
	if rest then lines = lines .. rest .. '\n' end
	cc = cc + string.len(lines)
	-- count words in the chunk
	local _, t = string.gsub(lines, "%S+", "")
	wc = wc + t
	-- count newlines in the chunk
	_, t = string.gsub(lines, "\n", "\n")
	lc = lc + t
end
print(lc, wc, cc)