Q:什么是”Simple Model”?

A:全部的文件操作都基于一个默认的输入文件和一个默认的输出文件。这就意味着同一时间对于输入和输出来说,仅仅可操作一个文件(默认的文件)。

默认的输入文件初始化是stdin,默认的输出文件初始化是stdout

-- "a.lua"文件里:
--[[ "io.read()"从"io.input()"所指定的默认输入文件里读;
"io.write()"向"io.output()"所指定的默认输出文件里写。 由于默认输入文件初始化是"stdin";默认的输出文件初始化是"stdout",
所以"io.read()"从"stdin"读;"io.write()"向"stdout"写。]]
io.write(io.read())
--[[ result:
> lua a.lua
Hello World!
Hello World!]]

io.read()io.write()都能够接收多个參数。

-- "a.lua"文件里:
io.write(io.read(), " and", " The Complete I/O Model.")
--[[ result:
> lua a.lua
The Simple I/O Model
The Simple I/O Model and The Complete I/O Model.]]

io.input()io.output()能够更改相应的默认文件。

-- "file1.txt"文件里:
Hello World!
-- "a.lua"文件里:
local temp_input = io.input() -- 保存原先的默认输入文件(此时是"stdin")。
local temp_output = io.output() -- 保存原先的默认输出文件(此时是"stdout")。
io.input("file1.txt") -- 指定默认的输入文件为"file1.txt"。
io.output("file2.txt") -- 指定默认的输出文件为"file2.txt"。
io.write(io.read())
io.input():close() -- 关闭"file1.txt"。
io.output():close() -- 关闭"file2.txt"。
io.input(temp_input) -- 恢复原先的默认输入文件。 io.output(temp_output) -- 恢复原先的默认输出文件。
--[[ result:
> lua a.lua
> cat file2.txt
Hello World!]]

Q:print()io.write()有什么差别?

A:

1、print()在打印其每一个參数之间添加\t,且最后添加\n。而io.write()均不会。

print("hello", "Lua")
print("Hi")
--[[ result:
hello Lua
Hi]]
io.write("hello", "Lua")
io.write("Hi", "\n")
--> helloLuaHi

2、io.write()io.output()指定的文件里写(初始值是stdout)。而print()总向stdout写。

-- "a.lua"文件里:
local temp = io.output()
io.output("file3.txt")
io.write("Hello")
print(" World")
io.output():close()
io.output(temp)
--[[ result:
"file3.txt"文件里:
Hello
"stdout":
World]]

3、print()对于不能被直接打印的对象会自己主动调用tostring()转换。而io.write()不会。

t = {}
print(t) --> table: 0x1ca9aa0
io.write(t) --> error

Q:io.read()的參数?

A:

-- "file1.txt"文件里:
line1
line2
line3 -- "data.txt"文件里:
6.0 -3.23 15e12
4.3 234 1000001

1、*a从当前的文件读取位置读取到文件尾,假设文件为空或者已在文件尾,则返回空串,

local temp = io.input()
io.input("file1.txt")
io.write(io.read("*a"))
io.input():close()
io.input(temp)
--[[ result:
line1
line2
line3]]

2、*l从当前的文件读取位置读取到行尾,不包括结尾的\n*L*l功能同样,但包括行尾的\n。假设文件为空或者已在文件尾,则返回nil

local temp = io.input()
io.input("file1.txt")
str = ""
local i = 1
while true do
local s
if i == 2 then
s = io.read("*L")
else
s = io.read("*l") -- "io.read()"不指定參数时。默认使用"*l"參数。
end
if s == nil then break end -- 当文件读取完时,返回"nil"。 str = str .. s
i = i + 1
end
io.write(str)
--[[ result:
line1line2
line3]]
io.input():close()
io.input(temp)

3、*n从当前的文件读取位置读取一个数值。io.read("*n")返回数值,而不是字符串。

当须要从一个文件里读取大量数字时,数字间的字符串为空格能够显著提高执行性能。io.read("*n")会跳过两个可被识别数字之间的随意空格。假设在当前位置找不到一个数字(由于格式不正确,或者是到了文件的结尾)。则返回nil

-- 打印文件每一行中最大的数字。
local temp = io.input()
io.input("data.txt")
while true do
local n1, n2, n3 = io.read("*n", "*n", "*n")
if not n1 then break end
print(math.max(n1, n2, n3))
end
io.input():close()
io.input(temp)
--[[ result:
15000000000000.0
1000001]]

4、io.read(num)能够指定一个数字,lua将尽可能读取”num”个字符(假设不够则能读多少读多少),假设文件为空或者已读取到文件尾则返回nil

local temp = io.input()
io.input("data.txt")
io.write(io.read(3)) -- 读取3个字符。
print()
-- 读取100个字符。 但实际没有那么多。所以能读取多少读多少。
io.write(io.read(100))
io.input():close()
io.input(temp)
--[[ result:
6.0
-3.23 15e12
4.3 234 1000001]]

Q:其它的”Simple Model”模式的库函数?

A:

--[[ io.tmpfile()
以"w+"的模式打开一个暂时文件,并返回其文件句柄。
当程序执行结束时,该文件会被自己主动删除。 ]]
local f = io.tmpfile()
f:write("123")
f:seek("set", 0) -- 将文件的当前读取位置定位到文件头。
print(f:read()) -- 123
f:close() --[[ io.type(obj)
检查"obj"是否为有效的文件句柄。 函数返回例如以下:
"file": 假设"obj"是一个被打开的文件句柄。
"closed file": 假设"obj"是一个被关闭的文件句柄。
nil: 假设"obj"不是一个有效的文件句柄。 ]]
print(io.type(f)) -- nil
f = io.tmpfile()
print(io.type(f)) -- file
f:close()
print(io.type(f)) -- closed file

附加:

1、io.input()io.output()在出错时均会报错。假设想控制错误。请使用”Complete model”下的io.open()

2、向io.write()传递的数字參数会被自己主动转换为字符串。

假设想全然控制转换,最好使用string.format()

io.write("sin(3) = ", math.sin(3), "\n")
--> sin(3) = 0.14112000805987
io.write(string.format("sin(3) = %.4f\n", math.sin(3)))
--> sin(3) = 0.1411

3、避免写io.write(a..b..c)这种代码。io.write(a, b, c)能够完成同样的工作,同一时候避免了..操作符复制字符串所耗费的内存资源。

4、当文件为空,或者文件当前的读取位置在文件尾时,io.read("*a")会返回空字符串,而io.read("*l")io.read("*L")io.read("*n")io.read(num)都会返回nil

5、假设想要逐行的读取文件,相比io.read("*l")。更好的选择是io.lines()io.lines()能够指定想要读取的文件。而且能够指定多个,依次的按行读取这些文件。

同一时候在读取完成后。io.lines()会自己主动关闭文件。

-- 为文件的每一行添加行号。
-- "file1.txt"文件里。
line1
line2
line3
-- "a.lua"文件里。 local count = 1
for line in io.lines("file1.txt") do
io.write(string.format("%6d ", count), line, "\n")
count = count + 1
end
--[[ result:
> lua a.lua
1 line1
2 line2
3 line3]]

然而假设io.lines()不带參数,则与io.read()的功能同样,同一时候在读取完成后不会关闭文件。

6、假设对于文件须要逐块的操作。但终于会操作整个文件。

那么最好使用io.read("*a")读取整个文件,然后使用string.gmatch()分解出须要操作的块。

-- 上面打印文件每一行中最大的数字的样例。
-- 须要处理的块。一个或多个非空格加上一个或多个空格……
local block = "(%S+)%s+(%S+)%s+(%S+)%s+"
--[[ "string.gmatch()"返回的结果是字符串形式的,
假设直接传递给"math.max()"会依照字符串形式比較大小,
所以须要使用"tonumber()"转换为数字。]]
for n1, n2, n3 in string.gmatch(io.read("*a"), block) do
print(math.max(tonumber(n1), tonumber(n2), tonumber(n3)))
end

省去了多次读文件的操作。效率会更高。

7、在Lua中复制文件的高效的方式,

local size = 2^13    -- good buffer size (8K)
while true do
local block = io.read(size)
if not block then break end
io.write(block)
end

8、io.read(0)測试是否到了文件尾。假设未到,则返回空字符串,假设到了则返回nil

高速掌握Lua 5.3 —— I/O库 (1)的更多相关文章

  1. 高速掌握Lua 5.3 —— 字符串库 (2)

    Q:模式匹配字符串的相关函数? A: --[[ string.find(s, pattern [, init [, plain]]) 在字符串"s"中查找第一个与匹配模式" ...

  2. Lua的string和string库总结

    Lua有7种数据类型,分别是nil.boolean.number.string.table.function.userdata.这里我总结一下Lua的string类型和string库,复习一下,以便加 ...

  3. Lua中的常用函数库汇总

    lua库函数 这些函数都是Lua编程语言的一部分, 点击这里了解更多. assert(value) - 检查一个值是否为非nil, 若不是则(如果在wow.exe打开调试命令)显示对话框以及输出错误调 ...

  4. Lua中的字符串函数库

    字符串库中的一些函数是非常简单的: string.len(s)          返回字符串s的长度:string.rep(s, n)      返回重复n次字符串s的串:你使用string.rep( ...

  5. 高速掌握Lua 5.3 —— Lua与C之间的交互概览

    Q:什么是Lua的虚拟栈? A:C与Lua之间通信关键内容在于一个虚拟的栈.差点儿全部的调用都是对栈上的值进行操作,全部C与Lua之间的数据交换也都通过这个栈来完毕.另外,你也能够使用栈来保存暂时变量 ...

  6. 高速掌握Lua 5.3 —— 扩展你的程序 (1)

    Q:怎样在C中将Lua作为配置文件语言使用? A: "config.lua"文件里: -- window size width = 200 height = 300 "m ...

  7. Lua中的table函数库

    table.concat(table, sep,  start, end) concat是concatenate(连锁, 连接)的缩写. table.concat()函数列出参数中指定table的数组 ...

  8. C++ Primer高速入门之五:有用的模板库

    更新:又一次排版代码格式 除上篇博客介绍的基本数据类型外,C++ 还定义了一个内容丰富的抽象数据类 型标准库. 包含 string 和 vector,它们分别定义了字符串和矢量(集合).string ...

  9. Git高速入门——Git安装、创建版本号库以及经常使用命令

    学习Git最全面的资料,在我看来是这本书--Pro Git,网上关于Git的教程有非常多,包含当中一些非常优秀的教程.这一系列的博客,主要是记录自己学习Git的经历.以及在这一过程中遇到的一些问题. ...

随机推荐

  1. [terry笔记]python FTP

    如下是作业,用python做一个ftp,主要利用socket. server端在linux下运行,在client端可以执行shell命令(静态的) 在client端输入get xxx,即可下载. 在c ...

  2. Spring+mybatis+struts框架整合的配置具体解释

    学了非常久的spring+mybatis+struts.一直都是单个的用他们,或者是两两组合用过,今天总算整合到一起了,配置起来有点麻烦.可是配置完一次之后.就轻松多了,那么框架整合配置具体解释例如以 ...

  3. DevExpress TreeList控件的复选框

    作者:jiankunking 出处:http://blog.csdn.net/jiankunking TreeList控件能够同一时候显示树结构和其它数据列,即在一个列上建立父子关系展开或收缩,同一时 ...

  4. kentico version history and upgrade

    Version history Kentico 10: November 30, 2016 Kentico 9: November 24, 2015 Kentico 8.2: January 6, 2 ...

  5. nyoj--203--三国志(迪杰斯特拉+背包)

    三国志 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 <三国志>是一款很经典的经营策略类游戏.我们的小白同学是这款游戏的忠实玩家.现在他把游戏简化一下,地图 ...

  6. [JZOJ 5437] [NOIP2017提高A组集训10.31] Sequence 解题报告 (KMP)

    题目链接: http://172.16.0.132/senior/#main/show/5437 题目: 题解: 发现满足上述性质并且仅当A序列的子序列的差分序列与B序列的差分序列相同 于是我们把A变 ...

  7. POJ 3342 树形DP+Hash

    这是很久很久以前做的一道题,可惜当时WA了一页以后放弃了. 今天我又重新捡了起来.(哈哈1A了) 题意: 没有上司的舞会+判重 思路: hash一下+树形DP 题目中给的人名hash到数字,再进行运算 ...

  8. xxx while the managed IDbConnection interface was being used: Login failed for user xxx

    Process cube的时候遇到如下错误.   Errors in the high-level relational engine. The following exception occurre ...

  9. jQuery学习(八)——使用JQ插件validation进行表单校验

    1.官网下载:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ 目录结构: 2.引入jquery库和validation插件 ...

  10. asp.net 连接字符串的多种写法

    一.使用OleDbConnection对象连接OLE DB数据源 1.连接Access 数据库 Access 2000: “provider=Microsoft.Jet.Oledb.3.5;Data ...