writefile

function writefile( filename, ... ) --> res, err

Description

Creates a file and writes the value of each of its arguments() to the file. The arguments must be strings or numbers.

Parameters

filename

Path to the file that should be created.

Strings and numbers that will be written into the file.

Return Values

res

true or nil if an error occurse.

err

A message if an error occurse otherwise nil.

Code

--ZFUNC-writefile-v1
local function writefile( filename, ... ) --> res, err
   local f, err = io.open( filename, "w" )
   if err then return nil, err end

   local wres, err = f:write( ... )
   if err then return nil, err end

   return f:close()
end

return writefile

Examples

local t = require( "taptest" )
local writefile = require( "writefile" )

res, err = writefile( "WriteFileXmpl.txt", "first\n", 2 )
t( res, true )
t( err, nil )

f = io.open( "WriteFileXmpl.txt" )
t( f:read( "*l" ), "first" )
t( f:read( "*n" ), 2 )
f:close()

os.remove( "WriteFileXmpl.txt" )

t()