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

**URL:** <https://forum.arduino.cc/t/file-close-sd/1221484>\
**Category:** 中文 (Chinese)\
**Created:** [February 8, 2024, 2:18pm UTC](https://forum.arduino.cc/t/file-close-sd/1221484 "2024-02-08T14:18:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stellar\_space](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@stellar\_space](https://forum.arduino.cc/u/stellar_space)\
**Post date:** [February 8, 2024, 2:18pm UTC](https://forum.arduino.cc/t/file-close-sd/1221484/1 "2024-02-08T14:18:50Z")

</div>

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

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

```arduino
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");
  }
}

```

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

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 9, 2024, 11:36am UTC](https://forum.arduino.cc/t/file-close-sd/1221484/2 "2024-02-09T11:36:40Z")

</div>

Split this topic on February 9, 2024, 11:36am UTC

10 posts were merged into an existing topic: [I can't write or reopen my file in sd card](https://forum.arduino.cc/t/i-cant-write-or-reopen-my-file-in-sd-card/1221497)

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [February 9, 2024, 11:36am UTC](https://forum.arduino.cc/t/file-close-sd/1221484/3 "2024-02-09T11:36:40Z")

</div>

Closed on February 9, 2024, 11:36am UTC


