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( "tapered" )
local writefile = require( "writefile" )

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

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

os.remove( "WriteFileXmpl.txt" )

t.done()