WiFiMCU Reference Book

File Module

The file system is based on spi flash embeded in WiFiMCU. The totoal storage capacity is 1280k [(1024+256)*1024] bytes.

Function list

Function Definition
file.format() Format file system, all stored data will be lost after format
file.open() Open or create a file
file.close() Close an opened file
file.write() Write data to an opened file
file.writeline() Write data to an opened file, with a ‘\n’ added at the tailed of data
file.read() Read data from an opened file
file.readline() Read a line data from an opened file
file.list() Get the file name and size list in file system
file.slist() Print the file name and size list on terminal
file.remove() Remove file
file.seek() Set the position of file pointer
file.flush() Clear file buffer
file.rename() Rename the file
file.info() Get the file system storage status
file.state() Get the opened file’s name and size
file.compile() Compile a Lua scripts file to lc file.
dofile() Run a file

file.format()

Description

Format file system, all stored data will be lost after format. It’s recommended Do not do any things while formatting.

Syntax

file.format()

Parameters

nil

Returns

nil

If formatting is done successfully, “format done” will be printed, else “format error” will be printed.

Examples

-file.format()

format done

file.open()

Description

Open or create a file.

Syntax

ret = file.open(filename,mode)

Parameters

filename: filename string to be created or opened. Directories are not supported yet.

mode: opened type:

  "r": read mode (the default parameter)

  "r+": update mode, all previous data is preserved

  "w": write mode

  "w+": update mode, all previous data is erased

  "a": append mode

  "a+": append update mode, previous data is preserved, writing is only allowed at the end of file

Returns

ret: true if succeed, else nil.

Examples

-file.open("test.lua","w+")

-file.write("This is a test")

-file.close()

file.close()

Description

Close an opened file.

Syntax

file.close()

Parameters

nil

Returns

nil

Examples

-file.open("test.lua","w+")

-file.write("This is a test")

-file.close()

file.write()

Description

Write data to an opened file.

Syntax

ret=file.write(data)

Parameters

data: The data to be wrote.

Returns

ret: true if succeed, else nil.

Examples

-file.open("test.lua","w+")

-file.write("This is a test")

-file.close()

file.writeline()

Description

Write data to an opened file, with a ‘\n’ added at the tailed of data.

Syntax

ret=file.writeline(data)

Parameters

data: The data to be wrote. A char ‘\n’ will be added at the end of data.

Returns

ret: true if succeed, else nil.

Examples

-file.open("test.lua","w+")

-file.writeline("This is a test")

-file.close()

file.read()

Description

Read data from an opened file.

Syntax

ret=file.read()

ret=file.read(num)

ret=file.read(endchar)

Parameters

if the parameter is nil, read all byte in file.

num: if a number is assigned, read the num bytes from file, or all rest data in case of end of file.

endchar: read until endchar or EOF is reached.

Returns

ret: the file data if succeed, else nil.

Examples

-file.open("test.lua","r")

-data=file.read()

-file.close()

-print(data)

This is a test

-file.open("test.lua","r")

-data=file.read(10)

-file.close()

-print(data)

This is a

-file.open("test.lua","r")

-data=file.read('e')

-file.close()

-print(data)

This is a te

file.readline()

Description

Read a line data from an opened file.

Syntax

ret=file.readline ()

Parameters

nil

Returns

ret: the file data if succeed, else nil.

Examples

-file.open ("test.lua","w+")

-file.writeline(“this is a test”)

-file.close()

-file.open ("test.lua","r")

-data=file.readline()

-print(data)

-This is a test

-file.close()

file.list()

Description

Get the file name and size list in file system.

Syntax

ft=file.list()

Parameters

nil

Returns

ft: a Lua table, in which the filename is the key, file size is the value.

Examples

-for k,v in pairs(file.list()) do print("name:"..k.." size(bytes):"..v) end

-name:test.lua size(bytes):15

file.slist()

Description

Print the file name and size list on terminal.

Syntax

file.slist()

Parameters

nil

Returns

nil

Examples

-file.slist()

test.lua size:15

file.remove()

Description

Remove file.

Syntax

file.remove(filename)

Parameters

filename: filename string to be removed.

Returns

nil

Examples

-file.remove (“test.lua”)

file.seek()

Description

Set the position of file pointer.

Syntax

fi = file.seek(whence, offset)

Parameters

whence: should be one of the following:

"set": base is position 0 (beginning of the file);

"cur": base is current position;(default value)

"end": base is end of file;

offset: default 0.

Returns

fi: the file pointer final position if succeed, else nil.

Examples

-file.open ("test.lua","r")

-file.seek("set",10)

-data=file.read()

-file.close

-print(data)

test

file.flush()

Description

Clear file buffer.

Syntax

ret = file.flush()

Parameters

nil

Returns

ret: true if succeed, else nil.

Examples

-file.open ("test.lua","r")

-file.flush ()

-file.close()

file.rename()

Description

Rename the file.

Syntax

ret=file.rename(oldname,newname)

Parameters

oldname: File name to be changed.

newname: New file name.

Returns

ret: true if succeed, else nil.

Examples

-file.slist()

test.lua size:14

-file.rename (‘test.lua’,’ testNew.lua’)

-file.slist()

testNew.lua size:14

file.info()

Description

Get the file system storage status.

Syntax

last,used,total = file.info()

Parameters

nil

Returns

last: free storage left in bytes.

used: used storage in bytes.

total: all allocated storage for file system in bytes.

Examples

-last,used,total = file.info()

-print(last,used,total)

1140500 2750 1143250

file.state()

Description

Get the opened file’s name and size

Syntax

fn,sz = file.state()

Parameters

nil

Returns

fn: filename.

sz: file size in bytes.

Examples

-file.open("testNew.lua","r")

-fn,sz = file.state()

-file.close()

-print(fn,sz)

testNew.lua 14

file.compile()

Description

Compile a Lua scripts file to lc file. The lc file will be named as the same name as the Lua file.

Syntax

file.compile(‘filename.lua’)

Parameters

filename.lua: file name of the Lua scripts.

Returns

nil.

Examples

-file.open("test.lua","w+")

-file.write("print('Hello world!')")

-file.close()

-file.compile("test.lua")

-file.slist()

test.lua size:21

test.lc size:100

dofile()

Description

Run a file. The file can be either a Lua scripts or a lc format file.

Syntax

dofile(‘filename.lua’)

dofile(‘filename.lc’)

Parameters

filename.lua: Lua scripts file.

filename.lc: a lc file

Returns

nil.

Examples

-dofile("test.lua")

Hello world!

-dofile("test.lc")

Hello world!