Creating a temporary file in lua
Asked Answered
L

1

16

I've looked the LuaFileSystem doc and didn't really understood how I could create a temporary file and write in it. Also, I'm not sure exactly where I can find the temp file I create.. In /tmp?

Here's how my function would look like:

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
Lehmbruck answered 29/4, 2014 at 8:56 Comment(0)
R
23

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.

Renovate answered 29/4, 2014 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.