Lua读写文件 Lua读写文件代码示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Lua读写文件 Lua读写文件代码示例

  2021-03-21 我要评论
想了解Lua读写文件代码示例的相关内容吗,在本文为您仔细讲解Lua读写文件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Lua,读写文件,下面大家一起来学习吧。

读写文件的模式:

复制代码 代码如下:

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们