文章目录
- 1 前言
- 2 代码
- 3 结果
1 前言
尝试在air105的根目录下创建文件并读写
2 代码
t4_fs_info.lua
function get_fs_info()log.info("fsize", fs.fsize("/luadb/main.luac"))log.info("fsstat", fs.fsstat(""))log.info("fsstat", fs.fsstat("/luadb/")) -- 注意不能用"/luadb",要加上斜杠
end-- 记录开机次数
function fs_test()f = io.open("/boot_time", "rb")c = 0if f thendata = f:read("*a")log.info("fs", "data", data, data:toHex())c = tonumber(data)f:close()endlog.info("fs", "boot count", c)c = c + 1f = io.open("/boot_time", "wb")--if f ~= nil thenlog.info("fs", "write c to file", c, tostring(c))f:write(tostring(c))f:close()--endif fs thenlog.info("fsstat", fs.fsstat(""))end
endfunction t4_fs_root_write()print("init")-- f = io.mkfs("youkai")f = io.open("/youkai", "wb")print(type(f))--if f ~= nil thenc = "hi,langzhao"log.info("fs", "write c to file", c)f:write(tostring(c))f:close()f = io.open("/youkai", "r")if f thendata = f:read("*a") -- *a表示文件,l表示行-- https://gitee.com/openLuat/LuatOS/blob/master/lua/src/loslib.c g_read()print("file data : ",data)f:close()end-- air 105根目录""下可以创建文件并读写
end
-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "fsdemo"
VERSION = "1.0.0"log.info("main", PROJECT, VERSION)-- sys库是标配
_G.sys = require("sys")
require("t4_fs_info")--添加硬狗防止程序卡死
wdt.init(15000)--初始化watchdog设置为15s
sys.timerLoopStart(wdt.feed, 10000)--10s喂一次狗get_fs_info()sys.taskInit(function()-- 每次开机,把记录的数值+1fs_test()t4_fs_root_write()while 1 dosys.wait(500)end
end)-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!