在lua中创建一个临时文件



我看过LuaFileSystem文档,不知道如何创建临时文件并在其中写入。此外,我不确定在哪里可以找到我创建的临时文件。。在/tmp中?

以下是我的功能:

do
   function upload_file(web)
      f =  -- creates a temporary file
      f:write(file.contents)     -- writes the content of the file uploaded in the temp file
      f:seek("set", 0)          -- we go back at the beginning
      s = f:read("*a")          -- read it out
      print (s)                 -- print it out
      f:close()                 -- close it
   end
end

标准Lua中有两种解决方案:

  • io.tmpfile,它返回一个临时文件的句柄。此文件在更新模式下打开,程序结束时会自动删除。

  • os.tmpname,它返回一个具有可用于临时文件的文件名的字符串。该文件必须在使用前显式打开,并在不再需要时显式删除。

最新更新