X file:read: Difference between revisions
From GiderosMobile
Created page with "__NOTOC__ <languages /> '''<translate>Available since</translate>:''' Gideros 2011.6<br/> '''<translate>Class</translate>:''' file<br/> === <transl..." |
No edit summary |
||
| (6 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
'''Available since:''' Gideros 2011.6<br/> | |||
''' | '''Class:''' [[X File]]<br/> | ||
''' | |||
=== Description === | |||
Reads a file according to given formats which specify what to read. | |||
<syntaxhighlight lang="lua"> | |||
(string) = file:read(format1,...) | |||
</syntaxhighlight> | |||
For each format, the function returns a string (or a number) with the characters read, or nil if it cannot read data with the specified format. | |||
When called without formats, it uses a default format that reads the entire next line (see below). | |||
"*n": reads a number; this is the only format that returns a number instead of a string. | The available formats are: | ||
*"*n": reads a number; this is the only format that returns a number instead of a string | |||
*"*a": reads the whole file, starting at the current position. On end of file, it returns the empty string | |||
*"*l": reads the next line (skipping the end of line), returning nil on end of file. This is the default format. | |||
*number: reads a string with up to this number of characters, returning nil on end of file. If number is zero, it reads nothing and returns an empty string, or nil on end of file. | |||
"*a" | '''note: format "*all" is also valid for "*a"''' | ||
=== Parameters === | |||
'''format1''': (string) format<br/> | |||
'''...''': (string) more optional formats '''optional'''<br/> | |||
=== Return values === | |||
'''Returns''' (string) read string from file<br/> | |||
=== Example === | |||
= | <syntaxhighlight lang="lua"> | ||
''' | local f = io.open(file, "rb") | ||
if f == nil then | |||
=== | return nil | ||
end | |||
local line=f:read("*all") | |||
f:close() | |||
</syntaxhighlight> | |||
{{File}} | {{X File}} | ||
Latest revision as of 20:18, 17 September 2026
Available since: Gideros 2011.6
Class: X File
Description
Reads a file according to given formats which specify what to read.
(string) = file:read(format1,...)
For each format, the function returns a string (or a number) with the characters read, or nil if it cannot read data with the specified format.
When called without formats, it uses a default format that reads the entire next line (see below).
The available formats are:
- "*n": reads a number; this is the only format that returns a number instead of a string
- "*a": reads the whole file, starting at the current position. On end of file, it returns the empty string
- "*l": reads the next line (skipping the end of line), returning nil on end of file. This is the default format.
- number: reads a string with up to this number of characters, returning nil on end of file. If number is zero, it reads nothing and returns an empty string, or nil on end of file.
note: format "*all" is also valid for "*a"
Parameters
format1: (string) format
...: (string) more optional formats optional
Return values
Returns (string) read string from file
Example
local f = io.open(file, "rb")
if f == nil then
return nil
end
local line=f:read("*all")
f:close()