为什么 lua 函数 io.write() 不起作用。它只在终端上显示结果,而不是写入文件
Why the lua function io.write() did not work. It only display the results on the terminal, rather than writing to a file
我正在学习 Lua IO 库。我在使用 io.write() 时遇到了问题。在Lua中的Programming Design中,有一段代码逐行遍历文件,并在每行前面加上序号。
这是我正在处理的文件:
test file: "iotest.txt"
这是我的代码
io.input("iotest.txt")
-- io.output("iotest.txt")
local count = 0
for line in io.lines() do
count=count+1
io.write(string.format("%6d ",count), line, "\n")
end
这是终端显示的结果,但是这个结果无法写入文件,我是否加了IO。是否输出("iotest.txt").
the results in terminal
这是文件的结果,我们可以看到没有变化
The result after code running
要写入文件,您需要一个文件句柄。
此句柄来自:io.open()
参见:https://www.lua.org/manual/5.4/manual.html#6.8
文件句柄具有作用于自身的方法。
那就是文件句柄 :
之后的函数。
所以 io.write()
在文件中 stdout
和 file:write()
上输出。
可以将定义的函数转储到文件的示例函数...
fdump=function(func,path)
assert(type(func)=="function")
assert(type(path)=="string")
-- Get the file handle (file)
local file,err = io.open(path, "wb")
assert(file, err)
local chunk = string.dump(func,true)
file:write(chunk)
file:flush()
file:close()
return 'DONE'
end
这些方法取自io.stdin
close = function: 0x566032b0
seek = function: 0x566045f0
flush = function: 0x56603d10
setvbuf = function: 0x56604240
write = function: 0x56603e70
lines = function: 0x566040c0
read = function: 0x56603c90
这使得它可以像...一样直接使用它
(Lua 控制台:lua -i )
> do io.stdout:write('Input: ') local result=io.stdin:read() return result end
Input: d
d
只需在写入操作后添加 io.flush()
即可将数据保存到文件中。
io.input("iotest.txt")
io.output("iotestout.txt")
local count = 0
for line in io.lines() do
count=count+1
io.write(string.format("%6d ",count), line, "\n")
end
io.flush()
io.close()
参考Lua 5.4 Reference Manual : 6.8 - Input and Output Facilities
io.flush()
会将任何写入的数据保存到您使用 io.output
设置的输出文件中
请参阅 koyaanisqatsi 对文件句柄的可选使用的回答。如果您同时处理多个文件并让您更好地控制如何与文件交互,这将变得特别有用。
也就是说你还应该有不同的输入和输出文件。您会同意交替读取和写入同一个文件没有意义。
您正试图打开同一个文件同时进行读写操作。你不能那样做。
有两种可能的解决方案:
- 从文件 X 读取,遍历它并将结果写入另一个文件 Y。
- 将完整的文件X读入内存,关闭文件X,然后删除文件X,打开相同的文件名进行写入,并在遍历原始文件(内存中)的同时写入它。
否则,您的方法是正确的,尽管 Lua 中的文件操作更经常使用 io.open() 和文件句柄而不是 io.write() 和 io.read 来完成().
我正在学习 Lua IO 库。我在使用 io.write() 时遇到了问题。在Lua中的Programming Design中,有一段代码逐行遍历文件,并在每行前面加上序号。 这是我正在处理的文件:
test file: "iotest.txt"
这是我的代码
io.input("iotest.txt")
-- io.output("iotest.txt")
local count = 0
for line in io.lines() do
count=count+1
io.write(string.format("%6d ",count), line, "\n")
end
这是终端显示的结果,但是这个结果无法写入文件,我是否加了IO。是否输出("iotest.txt").
the results in terminal
这是文件的结果,我们可以看到没有变化
The result after code running
要写入文件,您需要一个文件句柄。
此句柄来自:io.open()
参见:https://www.lua.org/manual/5.4/manual.html#6.8
文件句柄具有作用于自身的方法。
那就是文件句柄 :
之后的函数。
所以 io.write()
在文件中 stdout
和 file:write()
上输出。
可以将定义的函数转储到文件的示例函数...
fdump=function(func,path)
assert(type(func)=="function")
assert(type(path)=="string")
-- Get the file handle (file)
local file,err = io.open(path, "wb")
assert(file, err)
local chunk = string.dump(func,true)
file:write(chunk)
file:flush()
file:close()
return 'DONE'
end
这些方法取自io.stdin
close = function: 0x566032b0
seek = function: 0x566045f0
flush = function: 0x56603d10
setvbuf = function: 0x56604240
write = function: 0x56603e70
lines = function: 0x566040c0
read = function: 0x56603c90
这使得它可以像...一样直接使用它
(Lua 控制台:lua -i )
> do io.stdout:write('Input: ') local result=io.stdin:read() return result end
Input: d
d
只需在写入操作后添加 io.flush()
即可将数据保存到文件中。
io.input("iotest.txt")
io.output("iotestout.txt")
local count = 0
for line in io.lines() do
count=count+1
io.write(string.format("%6d ",count), line, "\n")
end
io.flush()
io.close()
参考Lua 5.4 Reference Manual : 6.8 - Input and Output Facilities
io.flush()
会将任何写入的数据保存到您使用 io.output
请参阅 koyaanisqatsi 对文件句柄的可选使用的回答。如果您同时处理多个文件并让您更好地控制如何与文件交互,这将变得特别有用。
也就是说你还应该有不同的输入和输出文件。您会同意交替读取和写入同一个文件没有意义。
您正试图打开同一个文件同时进行读写操作。你不能那样做。
有两种可能的解决方案:
- 从文件 X 读取,遍历它并将结果写入另一个文件 Y。
- 将完整的文件X读入内存,关闭文件X,然后删除文件X,打开相同的文件名进行写入,并在遍历原始文件(内存中)的同时写入它。
否则,您的方法是正确的,尽管 Lua 中的文件操作更经常使用 io.open() 和文件句柄而不是 io.write() 和 io.read 来完成().