writelines

function writelines( filename, strlst ) --> res, err

Description

Writes each string list entry as line to a file.

Parameters

filename

Path to the file that should be created.

strlst

String list where strings are allowed.

Return Values

res

true or nil if an error occurse.

err

A message if an error occurse otherwise nil.

Code

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

   for _, str in ipairs( strlst ) do
      local wres, err = f:write( str, "\n" )
      if err then return nil, err end
   end

   return f:close()
end

return writelines

Examples

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

res, err = writelines( "WriteLinesXmpl.txt", { "first line", "", "third line" } )
t( res, true )
t( err, nil )

f = io.open( "WriteLinesXmpl.txt" )
t( f:read( "*l" ), "first line" )
t( f:read( "*l" ), "" )
t( f:read( "*l" ), "third line" )
f:close()

os.remove( "WriteLinesXmpl.txt" )

t()