File System (SPIFFS): Simultaneous r/w does not work

cokutau
Posts: 2
Joined: Thu Oct 05, 2017 7:40 am

File System (SPIFFS): Simultaneous r/w does not work

Postby cokutau » Thu Oct 05, 2017 7:43 am

Hi.

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();
	}

}
Any indication is appreciated.

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: File System (SPIFFS): Simultaneous r/w does not work

Postby tele_player » Thu Oct 05, 2017 11:27 pm

Those three defines aren't enough to properly use files. At the least, a few more should be added.

The Arduino library passes the text ("r", "w", etc.) to the SDK open() function, which actually implements the file mode semantics of POSIX fopen(), so you can use "w+", "r+", etc.

It's important to read the documentation to understand how to create, truncate, and append. On a Linux or Mac system - 'man fopen'. Or just Google 'man fopen'.

In my quick test, your program took minor changes to work correctly.

NOTE: First, before any of these will work, SPIFFS.format() must be called once to create the file system. This only needs to be done once, or whenever you want to clear the filesystem.


See below, my changes are commented with ***:

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+")) {    // *** "w+" create or truncate, R/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+")) {	// ***  "r+" open an existing file for R/W, don't truncate or create file
      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();

      file.seek(0, SeekSet); // *** rewind 
      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();
   }

}

cokutau
Posts: 2
Joined: Thu Oct 05, 2017 7:40 am

Re: File System (SPIFFS): Simultaneous r/w does not work

Postby cokutau » Fri Oct 06, 2017 5:59 am

Thank you very much tele_player, now it is clear.
I tried it again and it works correctly.

Who is online

Users browsing this forum: No registered users and 21 guests