io.tmpfile()

Type Function
Library io.*
Return value Object
Revision Release 2024.3703
Keywords io, tmp, temporary file, file
See also io.read()
io.open()

Overview

Opens a temporary file for reading and writing and returns a handle to it. When the app ends normally, this file will be deleted.

Note that calling this function creates an empty file. Use the file:seek() function to reset the position in the file for reading back the data. The data will be lost if you close and call io.tmpfile() to open it again.

Gotchas

io.tmpfile() seems to always return nil on Android, since Android OS discourages the use of this function for both sandboxing and partition space issues.

Syntax

io.tmpfile()

Example

local fh = io.tmpfile()  -- Create an empty temporary file
fh:write( "My temporary file data" )
fh:flush()  -- Ensure data written to file
print( "file position: ", fh:seek() )
 
fh:seek( "set", 0 )  -- Reset file position
local content = fh:read( "*a" )  -- Read all the file
print( "File content: " .. content )