The FS has these three modes:
#define FILE_READ "r"
#define FILE_WRITE "w"
#define FILE_APPEND "a"
I have tried all of them, even those that did work with ESP8266 ("r +", "w +", etc.) and I can not read and write at one time in a file. If I open it in write mode I can not read, and vice versa.
Is there any way to open a file in read/write mode?
This is the test code I am using.
Code: Select all
#include <SPIFFS.h>
#include <FS.h>
void loop() {}
void setup() {
Serial.begin(115200);
SPIFFS.begin();
const char *fileName = "/foo.txt";
SPIFFS.remove(fileName);
if (File file = SPIFFS.open(fileName, "w")) {
Serial.printf("Phase 1: Writing...\r\n");
for (int8_t i = 0; i < 10; i++) {
uint8_t c = 'A' + i;
Serial.printf("pos[%d] write[%c] error[%d]\r\n", i, c, file.write(c) != 1);
file.flush();
}
Serial.printf("Phase 1: Reading...\r\n");
file.seek(0, SeekSet);
for (int8_t i = 0; i < 10; i++) {
int16_t c = file.read();
Serial.printf("pos[%d] read[%c] error[%d]\r\n", i, c, c == -1);
}
file.close();
}
if (File file = SPIFFS.open(fileName, "r+")) {
uint8_t pos = 5;
uint8_t c = 'X';
Serial.printf("Phase 2: Writing...\r\n");
file.seek(pos, SeekSet);
Serial.printf("pos[%d] write[%c] error[%d]\r\n", pos, c, file.write(c) != 1);
file.flush();
Serial.printf("Phase 2: Reading...\r\n");
for (int8_t i = 0; i < 10; i++) {
int16_t c = file.read();
Serial.printf("pos[%d] read[%c] error[%d]\r\n", i, c, c == -1);
}
file.close();
}
}