Write file in SD Card using PlatformIO in VS Code

Hi, I am newbie to this field. I tried to write a file into a SD card by using PlatformIO and doit esp32 devkit board. The result appears sucess. However, it didn’t work, I can’t see any files in the SD card after all. My code is down below. Thanks for your help.

#include “FS.h”
#include “SD.h”
#include “SPI.h”

void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf(“Writing file: %s\n”, path);

File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println(“Failed to open file for writing”);
return;
}
if(file.print(message)){
Serial.println(“File written”);
} else {
Serial.println(“Write failed”);
}
file.close();
}

void setup(){
Serial.begin(115200);
if(!SD.begin(5)){
Serial.println(“Card Mount Failed”);
return;
}
uint8_t cardType = SD.cardType();

if(cardType == CARD_NONE){
Serial.println(“No SD card attached”);
return;
}

Serial.print("SD Card Type: ");
if(cardType == CARD_MMC){
Serial.println(“MMC”);
} else if(cardType == CARD_SD){
Serial.println(“SDSC”);
} else if(cardType == CARD_SDHC){
Serial.println(“SDHC”);
} else {
Serial.println(“UNKNOWN”);
}

uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf(“SD Card Size: %lluMB\n”, cardSize);

}

void loop(){
writeFile(SD, “hello.txt”, "hello ");
Serial.printf(“write done”);
}

You’re writing to a FAT16 filesystem over and over again with no pause. That’s not good. Try to put the code as last thing in setup() to only have it execute once. Then check if the file is there.