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

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

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

os.remove( "WriteLinesXmpl.txt" )

t.done()