Lua读写文件代码示例
程序员文章站
2022-07-05 11:04:09
读写文件的模式:
复制代码 代码如下:
r - 读取模式w - 写入模式(覆盖现有内容)
a - 附加模式(附加在现有内容之后)
b -...
读写文件的模式:
复制代码 代码如下:
r - 读取模式w - 写入模式(覆盖现有内容)
a - 附加模式(附加在现有内容之后)
b - 二进制模式
r+ - 读取更新模式(现有数据保留)
w+ - 写入更新模式(现有数据擦除)
a+ - 附加更新模式(现有数据保留,只在文件末尾附加)
do --read data from file function readfile() local filehandle = assert(io.open("test.txt", "r"), "not the file"); if filehandle then local outdata = filehandle:read("*all"); print(outdata); else print("false"); end filehandle:close(errorinfo); end --write data to the file function writefile(databuffer) local writehandle = assert(io.open("write.txt", "a+"), "not the file"); if writehandle then writehandle:write(databuffer); print("true"); else print("false"); end writehandle:close(); end local inputdata = 0; repeat inputdata = io.read(); --write the data from io writefile(inputdata); until inputdata == '#' end