Fs.open/Mode:Read

From ComputerCraft Wiki
Revision as of 17:01, 19 December 2012 by Lyqyd (Talk | contribs) (Initial rough entry.)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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


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