How to read LittleFS file to std::string (using Arduino/PlatformIO)
The following utility function reads . As a prerequisite, you need to initialize the LittleFS filesystem and configure PlatformIO to use LittleFS as filesystem when uploading the filesystem image.
Note*:*Before using any of the functions below, you need to call InitFilesystem()
in setup()
in order to mount the filesystem. You can find this function in our previous post on how to initialize LittleFS.
#include <string>
std::string ReadFileToString(const char* filename) {
auto file = LittleFS.open(filename, "r");
size_t filesize = file.size();
// Read into temporary Arduino String
String data = file.readString();
// Don't forget to clean up!
file.close();
return std::string(data.c_str(), data.length());
}
Example usage:
std::string cert = ReadFileToString("/cert.pem");