Difference between revisions of "Fs.open/Mode:Read"
From ComputerCraft Wiki
(Initial rough entry.) |
(Categorised page) |
||
(One intermediate revision by one other user not shown) | |||
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> | ||
+ | }} | ||
+ | |||
+ | [[Category:Lua_Core_Functions]] |
Latest revision as of 09:42, 25 March 2014
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
![]() | |
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