无法再File.close()后再次打开SD卡中的文件

论坛里的各位好,我是一个新手,今天我在使用SD卡模块对一块FAT格式的512MB SD卡尝试进行读写操作时,发生了如题的问题

我使用的代码来自Arduino IDE中的示例->SD->ReadWrite,其中,有关两次写入操作的代码如下:

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
  // 第一次写入,没有报错,但文件只是被创建但没有被写入“testing 1,2,3”
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  // 第二次写入,报错
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

我已经重复确认了我的硬件连接是正确的,对于这个问题我已经用了一下午的时间尝试解决,我希望能够得到帮助,谢谢!

10 posts were merged into an existing topic: I can't write or reopen my file in sd card