如何将 LittleFS 文件读取为 std::string (使用 Arduino/PlatformIO)
以下实用函数读取。作为先决条件,你需要初始化 LittleFS 文件系统并在上传文件系统镜像时配置 PlatformIO 使用 LittleFS 作为文件系统。
注意*:*在使用以下任何函数之前,你需要在 setup() 中调用 InitFilesystem() 以挂载文件系统。你可以在我们之前关于如何初始化 LittleFS 的文章中找到此函数。
ReadFileToString.cpp
#include <string>
std::string ReadFileToString(const char* filename) {
auto file = LittleFS.open(filename, "r");
size_t filesize = file.size();
// 读取到临时 Arduino String
String data = file.readString();
// 不要忘记清理!
file.close();
return std::string(data.c_str(), data.length());
}示例用法:
example_usage.cpp
std::string cert = ReadFileToString("/cert.pem");Check out similar posts by category:
C/C++, ESP8266/ESP32, PlatformIO
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow