Difference between revisions of "Fs.open/Mode:Read"

From ComputerCraft Wiki
Jump to: navigation, search
(Initial rough entry.)
 
Line 16: Line 16:
 
|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
 
|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
 
|desc=Reads the next line from the file
 
|desc=Reads the next line from the file
}}
+
|examples=<pre>
 
+
<pre>
+
 
local handle = fs.open("file/name", "r")
 
local handle = fs.open("file/name", "r")
 
if handle then
 
if handle then
Line 30: Line 28:
 
end
 
end
 
</pre>
 
</pre>
 +
}}

Revision as of 21:37, 30 December 2012

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