Skip to content

Commit ae1bee6

Browse files
Re #1: examples and corrections
1 parent d9eed47 commit ae1bee6

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

lib/io.md

+42
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,45 @@ The **io** library has been simplified and only a subset of functions and their
88
* [io.read()](io_read.md)
99
* [io.write()](io_write.md)
1010
* [io.seek()](io_seek.md)
11+
12+
13+
## Examples
14+
15+
### Read whole file
16+
17+
```lua
18+
-- this is an OpenTX stand-alone script
19+
20+
local function run(event)
21+
print("lua io.read test") -- print() statements are visible in Debug output window
22+
local f = io.open("foo.bar", "r")
23+
while true do
24+
local data = io.read(f, 10) -- read up to 10 characters (newline char also counts!)
25+
if #data == 0 then break end -- we get zero lenght string back when we reach end of the file
26+
print("data: "..data)
27+
end
28+
io.close(f)
29+
return 1
30+
end
31+
32+
return { run=run }
33+
```
34+
35+
### Append data to file
36+
37+
```lua
38+
-- this is an OpenTX stand-alone script
39+
40+
local function run(event)
41+
print("lua io.write test")
42+
local f = io.open("foo.bar", "a") -- open file in append mode
43+
io.write(f, "first line\r\nsecond line\r\n")
44+
io.write(f, 4, "\r\n", 35.6778, "\r\n") -- one can write multiple items at the same time
45+
local foo = -4.45
46+
io.write(f, foo, "\r\n")
47+
io.close(f)
48+
return 1 -- this will end the script execution
49+
end
50+
51+
return { run=run }
52+
```

lib/io_open.md

-14
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,3 @@ The io.open() function is used to open the file on SD card for subsequent readin
2222
* `<file object>` if file was successfully opened.
2323

2424
* `nil` if file could not be opened.
25-
26-
27-
### io.seek(fd, offset)
28-
`fd` file object
29-
`offset` is always based from the beginning of the file. If offset is bigger than the file size, the pointer is moved to the end of the file.
30-
Other standard seek bases (like "cur", "end") are not supported.
31-
32-
### io.read(fd, length)
33-
`fd` file object
34-
`length` number of characters to read.
35-
36-
Other read *commands* (like "*all") are **not supported**.
37-
38-
Return: string, number of characters returned is <= length. If end of file is reached, then empty string is returned.

0 commit comments

Comments
 (0)