当前位置:网站首页>[luat-air105] 4.1 file system FS

[luat-air105] 4.1 file system FS

2022-07-05 03:27:00 Birds hate fish

List of articles

1 Preface

Try to air105 Create files under the root directory of and read and write

2 Code

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/"))    --  Be careful not to use "/luadb", To add a slash 
end

--  Record startup times 
function fs_test()
    f = io.open("/boot_time", "rb")
    c = 0
    if f then
        data = f:read("*a")
        log.info("fs", "data", data, data:toHex())
        c = tonumber(data)
        f:close()
    end
    log.info("fs", "boot count", c)
    c = c + 1
    f = io.open("/boot_time", "wb")
    --if f ~= nil then
    log.info("fs", "write c to file", c, tostring(c))
    f:write(tostring(c))
    f:close()
    --end

    if fs then
        log.info("fsstat", fs.fsstat(""))
    end
end

function t4_fs_root_write()
    print("init")
    -- f = io.mkfs("youkai")
    f = io.open("/youkai", "wb")
    print(type(f))
    --if f ~= nil then
    c = "hi,langzhao"
    log.info("fs", "write c to file", c)
    f:write(tostring(c))
    f:close()

    f = io.open("/youkai", "r")
    if f then
        data = f:read("*a") -- *a Said file ,l Said line 
        -- https://gitee.com/openLuat/LuatOS/blob/master/lua/src/loslib.c g_read()
        print("file data : ",data)
        f:close()
    end

    -- air 105 root directory "" You can create files and read and write 
end

-- LuaTools need PROJECT and VERSION These two messages 
PROJECT = "fsdemo"
VERSION = "1.0.0"

log.info("main", PROJECT, VERSION)

-- sys The library is standard 
_G.sys = require("sys")
require("t4_fs_info")


-- Add a hard dog to prevent the program from getting stuck 
wdt.init(15000)-- initialization watchdog Set to 15s
sys.timerLoopStart(wdt.feed, 10000)--10s Feed the dog once 

get_fs_info()

sys.taskInit(function()

    --  Every time you turn it on , Record the value +1
    fs_test()

    t4_fs_root_write()

    while 1 do
        sys.wait(500)
    end
end)

--  User code ended ---------------------------------------------
--  The end is always this sentence 
sys.run()
-- sys.run() Don't add any statements after !!!!!

3 result

 Insert picture description here

原网站

版权声明
本文为[Birds hate fish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140743126227.html