SdFat icon indicating copy to clipboard operation
SdFat copied to clipboard

Create a new file after format

Open DriekdeGadgetfreak opened this issue 3 years ago • 3 comments
trafficstars

Hi,

Trying to create a file after format does not work.

SdFat.format(); SdFat.open("xxx.bat", FILE_WRITE);

format works OK. After reboot I can open a new file without problems.

DriekdeGadgetfreak avatar Jul 11 '22 14:07 DriekdeGadgetfreak

After format you need to do sd.begin(). Format may change the card layout.

Here is an example of how I would handle the case where I want to format the card before using it.

#include "SdFat.h"
SdFat sd;

#define SD_CS_PIN SS
File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {}
  Serial.println("Type any character to begin");
  while (!Serial.available()) {}

  // Use cardBegin in case the format is bad.
  if (!sd.cardBegin(SdSpiConfig(SD_CS_PIN))) {
    Serial.println("cardBegin failed");
    return;
  }
  Serial.println("Formatting card");
  // Serial not required - good for debug.
  if (!sd.format(&Serial)) {
    Serial.println("format failed");
    return;    
  }
  if (!sd.begin(SD_CS_PIN)) {
    Serial.println("begin failed!");
    return;
  }
  myFile = sd.open("xxx.bat", FILE_WRITE);
  if (!myFile) {
    Serial.println("open failed!");
    return;
  }
  myFile.println("Test line.");
  myFile.close();
  sd.ls(LS_SIZE);
  Serial.println("Done");
}
void loop() {}

Here is expected output:

Type any character to begin Formatting card Writing FAT ................................ Format Done 12 xxx.bat Done

greiman avatar Jul 11 '22 15:07 greiman

No, does not seem to work here...

DriekdeGadgetfreak avatar Jul 11 '22 16:07 DriekdeGadgetfreak

OK...

greiman avatar Jul 11 '22 17:07 greiman