Store/Read String from FileSystem

Hi...

For persistence, I am trying to store a string into a file on the OS. The string is of varying size, I will limit it to 128 characters.

My code:

int storeString(String data, String fileName) {
  int f = open(fileName, O_RDWR | O_CREAT | O_TRUNC);
  if (f != -1) {
    write(f, data.c_str(), data.length());
    close(f);
    return data.length();
  } else {return -1;}
}

And to read:

int readString(String fileName, void* buf) {
  int f = open(fileName, O_RDWR | O_CREAT | O_TRUNC);
  if (f != -1) {
    int err = read(f, buf, 128);
    close(f);
    return err;
  } else {return -1;}
}

(Note, the 1024 bytes read size is just to make sure it's large enough)

I think my problem is going to be the read back. I don't know Strings/Character buffers well in C, so I think I need a little help.

  • How do I declare the buffer for the read?
  • How do I pass that into readString()?
  • How do I then cast the result back to a Particle String?

Basically, can someone confirm that my prototype is correct, and then show me how to use it? :wink:

I am trying to use chatGPT for help, but I can't get my keywords right!

This is how I would do it. This code compiles but I didn't run it on a device but it's probably close.


#include <fcntl.h>
#include <sys/stat.h>

int storeString(const char *fileName, const String &data)
{
    int resultLen = -1;

    int fd = open(fileName, O_RDWR | O_CREAT | O_TRUNC);
    if (fd != -1) {
        resultLen = write(fd, data.c_str(), data.length());
        close(fd);
    }
    return resultLen;
}

int readString(const char *fileName, String &result)
{
    int resultLen = -1;

    result = "";

    int fd = open(fileName, O_RDONLY);
    if (fd != -1) {
        struct stat sb = {0};
        fstat(fd, &sb); 
        resultLen = sb.st_size;

        if (resultLen > 0) {
            char *buf = new char[resultLen + 1];
            if (buf) {
                resultLen = read(fd, buf, resultLen);
                if (resultLen >= 0) {
                    buf[resultLen] = 0;
                    result = buf;
                }
    
                delete[] buf;
            }
        }
        else {
            resultLen = 0;
        }

        close(fd);
    }
    
    return resultLen;
}
  • The filename is const char * instead of String because it won't be modified. You can still pass a String as a parameter if you want when calling it.
  • I changed readString to take a String& (String by reference) which is filled in with the data. This eliminates the need to pre-define how big of a buffer to use.
  • The inside of readString() uses fstat to determine the length of the file, which determines how much buffer is needed.

Thank you!

The storage seems to work. I get a positive byte count back.

But I am not getting anything back with the read of the same file.

  1. Do I need to pass a pointer to my String buffer, or just the variable?

  2. Looking at that code, it should not matter, no? That is, I should still see a non-zero file size, even if I mess up copying the data to the string?

Nevermind, I made an error copying the code.

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