Fork me on GitHub

File

Mongoose OS provides VFS layer and thus can mount multiple filesystems. For example, on ESP32 it can mount SPIFFS and SD Card FAT FS. By default, SPIFFS is used, with an exception of CC3200 where native SimpleLink FS is used.

When firmware is built, all files from the fs/ directory are copied to the filesystem, plus few files that store device configuration.

The C/C++ API for the filesystem is ISO C fopen(), fclose(), fread(), fwrite(), remove():

FILE *fp = fopen("data.txt", "r");
if (fp != NULL) {
    ...
    fclose(fp);
}

JAVASCRIPT API Reference api_file.js

load("api_file.js");


File.read(name)

Read the whole file into a string variable.

Return value: a string contents of the file. If file does not exist, an empty string is returned.

Example: read a .json configuration file into a config object:

let obj = JSON.parse(File.read('settings.json'));

File.remove(name)

Delete file with a given name. Return value: 0 on success, non-0 on failure.

File.rename(old, new)

Rename file old to new. Return 0 on success, non-0 on failure.

File.write(str, name, mode)

Write string str into file name.

If file does not exist, it is created. mode is an optional file open mode argument, 'w' by default, which means that previous content is deleted. Set mode to 'a' in order to append to the existing content. Return value: number of bytes written.

Example - write a configuration object into a file:

File.write(JSON.stringify(obj, 'settings.json'));