Skip to main content

Get a specific line from a text file (file.get_line)

Declaration

line, err = file.get_line(path, n)

Parameters

  • path
    String. Absolute file path.
  • n
    Integer. Line number. 0 means last line + 1. Negative means counting from the end.

Returns

  • line
    String or nil. Empty string if line number is out of range. nil if file does not exist.
  • err
    String. Error message when operation fails.

Notes

Get a specific line from a text file.
This function automatically strips UTF-8 BOM at the file head.

Example

local data, err = file.get_line("/var/mobile/1.txt", 1)
if data then
sys.alert("First line of /var/mobile/1.txt is "..data)
else
sys.alert("Read failed: "..err)
end

Note: Uses sys.alert