Io.input: Difference between revisions
No edit summary |
No edit summary |
||
| (3 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
'''Available since:''' Gideros 2011.6<br/> | |||
''' | '''Class:''' [[io]]<br/> | ||
''' | |||
=== Description === | |||
When called with a file name, it opens the named file (in text mode), and sets its handle as the default input file. | |||
When called with a file handle, it simply sets this file handle as the default input file. | |||
< | |||
When called without parameters, it returns the current default input file. | |||
<syntaxhighlight lang="lua"> | |||
(file) = io.input(file) | (file) = io.input(file) | ||
</ | </syntaxhighlight> | ||
=== | |||
'''file''': (file) | '''In case of errors this function raises the error, instead of returning an error code.''' | ||
=== | |||
'''< | === Parameters === | ||
'''file''': (file) file object to use as default input '''optional'''<br/> | |||
=== Return values === | |||
'''Returns''' (file) default input file object<br/> | |||
=== Example === | |||
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:47, 20 August 2026
Available since: Gideros 2011.6
Class: io
Description
When called with a file name, it opens the named file (in text mode), and sets its handle as the default input file.
When called with a file handle, it simply sets this file handle as the default input file.
When called without parameters, it returns the current default input file.
(file) = io.input(file)
In case of errors this function raises the error, instead of returning an error code.
Parameters
file: (file) file object to use as default input optional
Return values
Returns (file) default input file object
Example
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)