Spiffs write not working

Hello,

I am trying to run this simple program on Arduino IDE in linux. But I am not sure why nothing is been written to my file. Can anyone please help me solve this issue?

I created newfile using the command on ubuntu and then renamed it to "test" using mv. My file is stored in sketch_oct27a folder, I also get "File was written" on the serial monitor after the program is uploaded but my file is empty.

#include "SPIFFS.h"
 
void setup() {
 
  Serial.begin(115200);
 
  if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }
 
  File file = SPIFFS.open("/sketch_oct27a/test.txt", FILE_WRITE);
 
  if (!file) {
    Serial.println("There was an error opening the file for writing");
    return;
  }
  if (file.print("TEST SPIFFS........")) {
    Serial.println("File was written");
  } else {
    Serial.println("File write failed");
  }
 
  file.close();
}
 
void loop() {}

Which device are you using?

Your code does not suggest that you ever try to read the file created on the device. The file is not created on your local hard drive btw.

I tried for both ESP32 and ESP32S2. I tried to read the file by opening the file from my folder.

By the way what to you mean by not created on local hard drive? I created a newfile using the command >>newfile on the shell. Is that the write method? Please give me some brief insight since I am new to this. Thank you

SPIFFS is a file system that works on the ESP32, you can mount the ESP32 on a computer - almost like a flash drive - and then copy files to and from it. You may look at the reference or a simplified guide.