object:lines()

Type Function
Object File
Library io.*
Return value Function (iterator)
Revision Release 2022.3683
Keywords lines, files
See also io.open()
io.lines()

Overview

Returns an iterator function that, each time it is called, returns a new line from the file.

This function is similar to io.lines() except the file must be open first (with io.open()) and does not automatically close at the end of the file.

Syntax

File:lines()

Example

-- Path for the file to read
local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )

-- Open the file handle
local file, errorString = io.open( path, "r" )

if not file then
    -- Error occurred; output the cause
    print( "File error: " .. errorString )
else
    -- Output lines
    for line in file:lines() do
        print( line )
    end
    -- Close the file handle
    io.close( file )
end

file = nil