Unable to write then read successfully using Filesystem

Hi guys, using 2.0.0 boron and trying out the Filesystem stat() method found here https://docs.particle.io/reference/device-os/firmware/boron/#file-system-stat

Below is the code I’m using to test with. There are 2 issues I’m experiencing.

Even when I write different content say I change

write(fd, “foobar”, 6); to write(fd, “something else”, 14);

I see get the same result from stats->st_size as 1823. I would expect this value to be different if the file contents are different.

The 2nd problem I have is when I go to read the file contents it just shows empty string. So it’s almost like the file contents aren’t being written correctly (which explains why the stat size is not changing).

Has anyone actually gotten a successful read and write using the FileSystem in boron devcie@2.0.0? I have a suspicion there is something wrong with my code (maybe how I’m opening the file handler with O_RDWR | O_CREAT | O_TRUNC) Any help is appreciated! Thanks!

Also, as a side note, if there are any suggestions, I’m all ears. Basically we want to write some data to a file when certain sensor events happen and cache that file locally. Eventually we want to Particle publish the contents of that file when the device is not busy. The problem we have is that our devices don’t always have cell phone signal so we want to cache the sensor data until we can publish to particle after regaining a cellular connection.

CODE BELOW:

#include <fcntl.h>
#include <string>
#include <dirent.h>

SerialLogHandler logHandler;

void read_data_file()
{
  struct stat* stats;
  int sd = stat("/data1.txt", stats);

  if (sd != 0) {
    return;
  }

  // this always returns 1823 for me, event if I change the file contents
  Log.info("stats %d\n", stats->st_size); 

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

  Log.info("fd %d", fd);

  if (fd != -1) {
    void *buffer;
    int rd = read(fd, buffer, 6);
    char* content = static_cast<char*> (buffer);  

    Log.info("rd %d", rd);
    close(fd);

    Log.info("got a msg buffer");

    // THIS IS JUST EMPTY, I would expect it to be "foobar"
    Log.info(content);
  }
}

void write_data_file()
{
  int fd = open("/data1.txt", O_RDWR | O_CREAT | O_TRUNC);

  if (fd != -1) {
      write(fd, "foobar", 6);   // write something different here
      close(fd);
      Log.info("writing a file out on event loop");
  }
}

void loop() {

  write_data_file();
  read_data_file();
  delay(10000);
}

void setup() {

}

The first issue I suspect is here

  struct stat* stats;
  int sd = stat("/data1.txt", stats);

while the example shows this

    struct stat statbuf;
    int result = stat(path, &statbuf);

You are only creating a non-initialized pointer which should point to a struct stat (but doesn't) while the example actually creates a buffer to hold the stats.
The stat() call would only populate that area with data but does not create it for you.

The same here

You are not providing a buffer but only a pointer that points somewhere random.

2 Likes

That makes sense and works. Thanks!

1 Like

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