Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a temporary file in lua

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
like image 504
hy0shi Avatar asked Apr 29 '14 08:04

hy0shi


People also ask

How do I write to a file in Lua?

Syntax to write data to the file in Lua: file_name specifies the name of the file to be opened. mode is the mode in which the file must be opened. The file can be opened in read mode(r), write mode(w) or append mode(a). file_descriptor:write() operation is used to write the contents to the file.

What does Io mean in Lua?

I/O library is used for reading and manipulating files in Lua. There are two kinds of file operations in Lua namely implicit file descriptors and explicit file descriptors.


1 Answers

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.

like image 192
lhf Avatar answered Oct 22 '22 11:10

lhf