Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Filesystem/examples/File_IO/File_IO.cpp
Views: 1799
//Simple File I/O example12#include <AP_HAL/AP_HAL.h>3#include <AP_BoardConfig/AP_BoardConfig.h>4#include <AP_Filesystem/AP_Filesystem.h>5#include <stdio.h>67//set according to the data size in file8#define BUFFER_SIZE 50910const AP_HAL::HAL& hal = AP_HAL::get_HAL();1112void setup();13void loop();1415static AP_BoardConfig board_config;1617int file_count = 0;18const char *dir_path = nullptr;19static void write_file(const char *);20static void read_file(const char *);2122void setup()23{24board_config.init();2526//creating a test directory to store all files27dir_path = "./test_dir";28hal.console->printf("Creating directory - %s\n", dir_path);29int ret = AP::FS().mkdir(dir_path);30if (ret == -1) {31if (errno == EEXIST) {32hal.console->printf("Directory - %s already exist\n", dir_path);33} else {34hal.console->printf("Failed to create directory %s - %s\n", dir_path, strerror(errno));35}36}37}3839void loop()40{41//creating 10 files in test directory42if (file_count == 10) {43hal.console->printf("Finished creating files\n\n");44hal.scheduler->delay(1000);45return;46}4748file_count += 1;49char file_path[100];50snprintf(file_path, sizeof(file_path), "%s/test_%d.txt", dir_path, file_count);51write_file(file_path);52hal.scheduler->delay(1000);53read_file(file_path);54hal.scheduler->delay(1000);55}5657static void write_file(const char *file_path)58{59char write_buf[BUFFER_SIZE];6061//creating or opening a file in write mode62const int open_fd = AP::FS().open(file_path, O_WRONLY | O_CREAT | O_TRUNC);63if (open_fd == -1) {64hal.console->printf("Open %s failed - %s\n", file_path, strerror(errno));65return;66}6768//writing to file69const char *s = "Hello_World";70hal.console->printf("Writing to file %s => %s_%d\n", file_path, s, file_count);71snprintf(write_buf, sizeof(write_buf), "%s_%d\n", s, file_count);72const ssize_t to_write = strlen(write_buf);73const ssize_t write_size = AP::FS().write(open_fd, write_buf, to_write);74if (write_size == -1 or write_size != to_write) {75hal.console->printf("Write failed - %s\n", strerror(errno));76return;77}7879//closing file after writing80AP::FS().close(open_fd);81}8283static void read_file(const char *file_path)84{85char read_buf[BUFFER_SIZE];8687//opening a file in read mode88const int open_fd = AP::FS().open(file_path, O_RDONLY);89if (open_fd == -1) {90hal.console->printf("Open %s failed\n", file_path);91return;92}9394//reading from file95const ssize_t read_size = AP::FS().read(open_fd, read_buf, sizeof(read_buf));96if (read_size == -1) {97hal.console->printf("Read failed - %s\n", strerror(errno));98return;99}100101hal.console->printf("Reading from file %s => %s\n", file_path, read_buf);102103//closing file after reading104AP::FS().close(open_fd);105}106107AP_HAL_MAIN();108109110