Fs.open/Mode:Read

From ComputerCraft Wiki
Jump to: navigation, search

Opening A File For Reading

To read data from a saved file, one must use either of the read modes with fs.open(). The two read modes are textual read mode ("r") and binary read mode ("rb"). Most work with reading data from files in ComputerCraft will be done with textual read mode.

All examples below will use a file at the path "file/name", which contains this text:

line 1
line 2
a third line

Textual Read Mode

Grid Redstone.png  Function handle.readLine
Reads the next line from the file
Syntax handle.readLine()
Returns string the next line read from the file, with the end-of-line character stripped; or nil if there are no more lines in the file
Part of ComputerCraft
API fs

Examples

local handle = fs.open("file/name", "r")
if handle then
    text = handle.readLine()
    --text now contains: line 1
    text = handle.readLine()
    --text now contains: line 2
    text = handle.readLine()
    --text now contains: a third line
    handle.close()
end