File
File class hierarchy
The File class provides access to the underlying storage of a file system in a generic manner. This acts as a buffer between the file system and any technical limitations of the physical storage medium. On the storage, each file is a blob of data referenced by a file name represented as a string. By providing a file name to the open
function in the File class, you can access the blob of data stored on disk.
The File API allows operations on stored data without ever needing to buffer the data in RAM. Because flash is several orders of magnitude cheaper than RAM, this can significantly increase the amount of data a device can store without significantly increasing the device's cost.
Note: To get and set a buffer to a file, jump to the read
and write
functions. In the File class, the read
and write
functions always read or write the entire file at a time.
A File operates like an infinitely long reel of tape. Each File object keeps track of its current position in the data, and this position updates as you read from or write to the file. As data is written to the File, the File grows unless the underlying storage runs out of memory. If supported by the underlying file system, you can explicitly move the File position with the seek
and rewind
functions.
Combining read
, write
, and seek
on a File allows both streaming and random access to data stored on external memory without necessarily buffering the file contents on the device's RAM.
File class reference
Public Member Functions | |
File () | |
Create an uninitialized file. More... | |
File (FileSystem *fs, const char *path, int flags=O_RDONLY) | |
Create a file on a filesystem. More... | |
virtual | ~File () |
Destroy a file. More... | |
virtual int | open (FileSystem *fs, const char *path, int flags=O_RDONLY) |
Open a file on the filesystem. More... | |
virtual int | close () |
Close a file. More... | |
virtual ssize_t | read (void *buffer, size_t size) |
Read the contents of a file into a buffer. More... | |
virtual ssize_t | write (const void *buffer, size_t size) |
Write the contents of a buffer to a file. More... | |
virtual int | sync () |
Flush any buffers associated with the file. More... | |
virtual int | isatty () |
Check if the file in an interactive terminal device. More... | |
virtual off_t | seek (off_t offset, int whence=SEEK_SET) |
Move the file position to a given offset from from a given location. More... | |
virtual off_t | tell () |
Get the file position of the file. More... | |
virtual void | rewind () |
Rewind the file position to the beginning of the file. More... | |
virtual off_t | size () |
Get the size of the file. More... | |
virtual int | truncate (off_t length) |
Truncate or extend a file. More... | |
virtual int | set_blocking (bool blocking) |
Set blocking or nonblocking mode of the file operation like read/write. More... | |
virtual bool | is_blocking () const |
Check current blocking or nonblocking mode for file operations. More... | |
virtual int | enable_input (bool enabled) |
Enable or disable input. More... | |
virtual int | enable_output (bool enabled) |
Enable or disable output. More... | |
virtual short | poll (short events) const |
Check for poll event flags You can use or ignore the input parameter. More... | |
bool | writable () const |
Definition depends on the subclass implementing FileHandle. More... | |
bool | readable () const |
Definition depends on the subclass implementing FileHandle. More... | |
virtual void | sigio (Callback< void()> func) |
Register a callback on state change of the file. More... |
File system example
/* mbed Microcontroller Library
* Copyright (c) 2006-2019 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include <stdio.h>
#include <errno.h>
#include <functional>
#include "BlockDevice.h"
// Maximum number of elements in buffer
#define BUFFER_MAX_LEN 10
#define FORCE_REFORMAT true
// This will take the system's default block device
BlockDevice *bd = BlockDevice::get_default_instance();
// Instead of the default block device, you can define your own block device.
// For example: HeapBlockDevice with size of 2048 bytes, read size 1, write size 1 and erase size 512.
// #include "HeapBlockDevice.h"
// BlockDevice *bd = new HeapBlockDevice(2048, 1, 1, 512);
// This example uses LittleFileSystem as the default file system
#include "LittleFileSystem.h"
LittleFileSystem fs("fs");
// Uncomment the following two lines and comment the previous two to use FAT file system.
// #include "FATFileSystem.h"
// FATFileSystem fs("fs");
// Set up the button to trigger an erase
InterruptIn irq(BUTTON1);
void erase() {
printf("Initializing the block device... ");
fflush(stdout);
int err = bd->init();
printf("%s\n", (err ? "Fail :(" : "OK"));
if (err) {
error("error: %s (%d)\n", strerror(-err), err);
}
printf("Erasing the block device... ");
fflush(stdout);
err = bd->erase(0, bd->size());
printf("%s\n", (err ? "Fail :(" : "OK"));
if (err) {
error("error: %s (%d)\n", strerror(-err), err);
}
printf("Deinitializing the block device... ");
fflush(stdout);
err = bd->deinit();
printf("%s\n", (err ? "Fail :(" : "OK"));
if (err) {
error("error: %s (%d)\n", strerror(-err), err);
}
}
static auto erase_event = mbed_event_queue()->make_user_allocated_event(erase);
// Entry point for the example
int main() {
printf("--- Mbed OS filesystem example ---\n");
// Setup the erase event on button press, use the event queue
// to avoid running in interrupt context
irq.fall(std::ref(erase_event));
// Try to mount the filesystem
printf("Mounting the filesystem... ");
fflush(stdout);
int err = fs.mount(bd);
printf("%s\n", (err ? "Fail :(" : "OK"));
if (err || FORCE_REFORMAT) {
// Reformat if we can't mount the filesystem
printf("formatting... ");
fflush(stdout);
err = fs.reformat(bd);
printf("%s\n", (err ? "Fail :(" : "OK"));
if (err) {
error("error: %s (%d)\n", strerror(-err), err);
}
}
// Open the numbers file
printf("Opening \"/fs/numbers.txt\"... ");
fflush(stdout);
FILE *f = fopen("/fs/numbers.txt", "r+");
printf("%s\n", (!f ? "Fail :(" : "OK"));
if (!f) {
// Create the numbers file if it doesn't exist
printf("No file found, creating a new file... ");
fflush(stdout);
f = fopen("/fs/numbers.txt", "w+");
printf("%s\n", (!f ? "Fail :(" : "OK"));
if (!f) {
error("error: %s (%d)\n", strerror(errno), -errno);
}
for (int i = 0; i < 10; i++) {
printf("\rWriting numbers (%d/%d)... ", i, 10);
fflush(stdout);
err = fprintf(f, " %d\n", i);
if (err < 0) {
printf("Fail :(\n");
error("error: %s (%d)\n", strerror(errno), -errno);
}
}
printf("\rWriting numbers (%d/%d)... OK\n", 10, 10);
printf("Seeking file... ");
fflush(stdout);
err = fseek(f, 0, SEEK_SET);
printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
if (err < 0) {
error("error: %s (%d)\n", strerror(errno), -errno);
}
}
// Go through and increment the numbers
for (int i = 0; i < 10; i++) {
printf("\rIncrementing numbers (%d/%d)... ", i, 10);
fflush(stdout);
// Get current stream position
long pos = ftell(f);
// Parse out the number and increment
char buf[BUFFER_MAX_LEN];
if (!fgets(buf, BUFFER_MAX_LEN, f)) {
error("error: %s (%d)\n", strerror(errno), -errno);
}
char *endptr;
int32_t number = strtol(buf, &endptr, 10);
if (
(errno == ERANGE) || // The number is too small/large
(endptr == buf) || // No character was read
(*endptr && *endptr != '\n') // The whole input was not converted
) {
continue;
}
number += 1;
// Seek to beginning of number
fseek(f, pos, SEEK_SET);
// Store number
fprintf(f, " %d\n", number);
// Flush between write and read on same file
fflush(f);
}
printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10);
// Close the file which also flushes any cached writes
printf("Closing \"/fs/numbers.txt\"... ");
fflush(stdout);
err = fclose(f);
printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
if (err < 0) {
error("error: %s (%d)\n", strerror(errno), -errno);
}
// Display the root directory
printf("Opening the root directory... ");
fflush(stdout);
DIR *d = opendir("/fs/");
printf("%s\n", (!d ? "Fail :(" : "OK"));
if (!d) {
error("error: %s (%d)\n", strerror(errno), -errno);
}
printf("root directory:\n");
while (true) {
struct dirent *e = readdir(d);
if (!e) {
break;
}
printf(" %s\n", e->d_name);
}
printf("Closing the root directory... ");
fflush(stdout);
err = closedir(d);
printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
if (err < 0) {
error("error: %s (%d)\n", strerror(errno), -errno);
}
// Display the numbers file
printf("Opening \"/fs/numbers.txt\"... ");
fflush(stdout);
f = fopen("/fs/numbers.txt", "r");
printf("%s\n", (!f ? "Fail :(" : "OK"));
if (!f) {
error("error: %s (%d)\n", strerror(errno), -errno);
}
printf("numbers:\n");
while (!feof(f)) {
int c = fgetc(f);
printf("%c", c);
}
printf("\rClosing \"/fs/numbers.txt\"... ");
fflush(stdout);
err = fclose(f);
printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
if (err < 0) {
error("error: %s (%d)\n", strerror(errno), -errno);
}
// Tidy up
printf("Unmounting... ");
fflush(stdout);
err = fs.unmount();
printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
if (err < 0) {
error("error: %s (%d)\n", strerror(-err), err);
}
printf("Mbed OS filesystem example done!\n");
}