在lua中创建一个临时文件

hy0*_*shi 10 filesystems upload lua file luafilesystem

我看过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
Run Code Online (Sandbox Code Playgroud)

lhf*_*lhf 14

There are two solutions in standard Lua:

  • io.tmpfile, which returns a handle for a temporary file. This file is opened in update mode and it is automatically removed when the program ends.

  • os.tmpname, which returns a string with a file name that can be used for a temporary file. The file must be explicitly opened before its use and explicitly removed when no longer needed.