Example of Boron write and read to memory using Filesystem

Does anyone have working code for reading and writing to the Boron memory with the Filesystem. There are a few posts where comments indicate why the posted code doesn’t work (e.g. Unable to write then read successfully using Filesystem ) but I can not find any working code. Brief, working code for writing and reading would be a great help.

thanks, john

@colemanjj did you review the Filesystem docs?

https://docs.particle.io/reference/device-os/api/file-system/

Also, @rickkas7 has a File Helper library you might find useful:

Thanks for the suggestion. I studied that earlier today and understood it very little.

I’m good at muddling through working code and disassembling it into parts if I can see the output. But lonely code snippets or functions that I can’t test without a code framework I find very difficult to understand what they are up to.

Also the file system API is POSIX based. The functions like open, read, write, close work basically the same as their counterparts on Unix-based operating systems.

I developed working code based almost entirely on kelt’s post Unable to write then read successfully using Filesystem . I made some tweeks based on other posts and some suggestions from AI. I commented the code to the extent that I could. This compiles and runs on a Boron, OS 6.1.1. I hope it is useful as a very simplistic start to using the filesystem.

STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY)); // I'm not sure
STARTUP(System.enableFeature(FEATURE_RESET_INFO)); // these 4 lines are 
SYSTEM_THREAD(ENABLED);   // needed since I think they are the defaults
SYSTEM_MODE(SEMI_AUTOMATIC);   // these days
#include <fcntl.h>
#include <string>
#include <dirent.h>

SerialLogHandler logHandler;
#define buffSize 80     // defined this so that I could easily change the buffer size
void read_data_file()
{
  struct stat statbuf;   // the original code had "struct stat* statbuf" which didn't work
  int sd = stat("/data1.txt", &statbuf);  // I think this gets the return code from doing the commant stat
                                    // my understanding of &statbuf is minimal but it works
  if (sd != 0) {   // checks to see if the file exists. if sd=0 then file information successfully obtained
    return;
  }

  // 
  Log.info("statbuf %ld\n", statbuf.st_size); // publishes the size of the file

  // read the file
  int fd = open("/data1.txt", O_RDONLY); // opens the file read only

  Log.info("fd %d", fd); // prints the return code from opening the file for read only

  if (fd != -1) {
    //void* buffer;   // this did not seem to work in the original code so replaced it with the next line
    char buffer[buffSize];   // create a buffer of size "buffSize"
    int rd = read(fd, buffer, sizeof(buffer)-1);  // read sizeof(buffer)-1 bytes of the file into "buffer"
    char* content = static_cast<char*> (buffer);  // not sure what this is for

    Log.info("rd %d", rd); // print the return code for reading the file
    close(fd);  // close the file

    Log.info("got a msg buffer"); // a message to the console

    Log.info(content); // print the data read from the file
    Log.info(buffer);  // also print the data fromt eh file
  }
}

void write_data_file()
{
  int fd = open("/data1.txt", O_RDWR | O_CREAT | O_TRUNC); // open the file to write and set pointer to beginning of the file

  if (fd != -1) {  // if opening the file was successful write something
      write(fd, "foabar or somethign else \n", buffSize);   // write something up to 80 char
      close(fd);  // clos eth efile
      Log.info("writing a file out on event loop");  // message tot eh console
  }
}

void loop() {

  write_data_file(); // write the data file
  read_data_file();  // read the data file
  delay(5000);   // pause between write and reads before starting over
}

void setup() {

} 

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.