fsutil_read_file

int fsutil_read_file(const char *path, uint32_t offset, uint32_t len,
                     void *dst, uint32_t *out_len)

Calls fs_open(), fs_read(), and fs_close() to open a file at the specified path, retrieve data from the file starting from the specified offset, and close the file and invalidate the file handle.

Arguments

ArgumentDescription
pathPointer to the directory entry to query
offsetPosition of the file's read pointer
lenNumber of bytes to attempt to read
dstDestination buffer to read into
out_lenOn success, the number of bytes actually read gets written here. Pass null if you don't care.

Returned values

Notes

This is a convenience function. It is useful when the amount of data to be read from the file is small (i.e., all the data read can easily fit in a single buffer).

Header file

#include "fs/fsutil.h"

Example

This example demonstrates reading a small text file in its entirety and printing its contents to the console.

int
print_status(void)
{
    uint32_t bytes_read;
    uint8_t buf[16];
    int rc;

    /* Read up to 15 bytes from the start of the file. */
    rc = fsutil_read_file("/cfg/status.txt", 0, sizeof buf - 1, buf,
                          &bytes_read);
    if (rc != 0) return -1;

    /* Null-terminate the string just read. */
    buf[bytes_read] = '\0';

    /* Print the file contents to the console. */
    console_printf("%s\n", buf);

    return 0;
}