我已经使用 ESP32 和 SPIFFS 有一段时间了。我的项目将涉及在用户需要时更改文件中特定行的内容。该文件将始终以相同的格式保存,因此我知道将更改哪一行。
我当前的文件是这样存储的:
Content inside file:
DeviceNmae
test@test.com.br
123456
button to read
uid from databa
internet ssid
internet pass
当用户在应用程序中更改互联网 ssid 时,我的 esp32 将从数据库中读取内容并检测更改。它将存储传入的更改并更新行。
例如,我将数据更改为“int ssid now”,数据库将读取并将“internet ssid”更改为“int ssid now”。我只想从那一行更新内容,但我什么也没找到。如果我没有通过更新找到解决方案,我将不得不从文件中删除所有内容并创建一个新内容以更改该行。
我像这样附加数据:
void funcClass::append_data(String funcName, char Text[]) {
file = SPIFFS.open("/esp_name.txt", FILE_APPEND);
while (connection_state == 1 and funcName == ""){
if (connection_state == 1 and funcName == "" and stop_loop == 0){
for (int i = 0; i < strlen(Text); i++){
char c = Text[i];
SerialBT.write(c);
}
SerialBT.write('\n');
}
stop_loop = 1;
if (SerialBT.available()){
while (SerialBT.available()) {
insert_chars = SerialBT.read();
funcName = String(funcName + insert_chars);
}
stop_loop = 0;
}
}
if (file.print(funcName)){
Serial.print("data was added: ");
Serial.println(funcName);
}else{
Serial.println("data was not added");
return;
}
file.close();
}
``