I can't write or reopen my file in sd card

*I am not a native English speaker. If you are confused about my expression, please let me know in the comments. Thx!

Today I'm trying to write some data to my sd card, but there's something wrong.

I'm using the example code named ReadWrite which provided by Arduino IDE,contains two write operations to the SD card

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);
  // The first time,no error,the file was created but no content was written to it
  // 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");
  }
  // The second,here comes the error
  // 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");
  }
}

I have confirmed my wiring is correct, I have really no idea how to fix this right now.

You already posted that in Chinese... (无法再File.close()后再次打开SD卡中的文件)

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum. It will help you get the best out of the forum in the future.

➜ do you want to close the Chinese topic or this one ?

Thank you.

论坛里的各位好,我是一个新手,今天我在使用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");
  }
}

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

请把串行监视器打印出的文本复制并粘贴在这里(使用代码标记)
描述你的电路

(自动翻译)

Serial Monitor:
Initializing SD card...initialization done.
Writing to test.txt...done.
error opening test.txt

Circuit:
An SD module with SPI interface.
My SD module has eight pins, namely GND, MISO, SCK, MOSI, CS, 5V, 3x3, and GND (arranged in order of pins). Among them, I did not wire the 3x3 pin and the GND next to it according to the online tutorial (it is said that there is an AMS117-3.3 module on my SD card, and if it is powered by 3x3, it will make it unstable).

Here is my wiring:
(SD Module) -- (UNO R3)
GND -- GND
5V -- 5V
CS -- 4
MOSI -- 11
SCK -- 12
MISO -- 13

BTW, My SD card should be fine. I have formatted it multiple times as FAT and read and write it in the computer's card reader (with the write protector turned off). Ican get the information about the card by using the example named CardInfo.

Try with the sdFat library.

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */

#include <SPI.h>
//#include <SD.h>
#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) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

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

  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  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);

  // 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", FILE_WRITE);
  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");
  }
}

void loop() {
  // nothing happens after setup
}

Here is the code provided by sdfat library and I've tested this code. But I still get the error:
"Initializing SD card...initialization done.
Writing to test.txt...done.
error opening test.txt
"

Before I saw your comment I have tried many times using sd library to fix this problem.
For many times, it returned error when it opened the file for the second time and the file was created but there is nothing in it.
But for once, there is something different. I tried using a different mode to open the file

myFile = SD.open("test.txt", O_WRITE);

I changed the mode from FILE_WRITE to O_WRITE. Then, when the program is ended and I'm checking out the file in the sd card. I found out that "12345678" was written in the file, but actually the result I expected was "123456789". Finally, I found out that in this mode, I can only write 8 characters to the file(encoded to ANSI).

So...eventually, I didn't make any progress in solving this problem

This is not for reading

Opening for writing positions the cursor at the end of the file

Try SdFat test code ➜ SdFat/examples/ReadWriteSdFat/ReadWriteSdFat.ino at master · Bodmer/SdFat · GitHub

What’s your hardware ? What’s your circuit ?

My Hardware:
Arduino UNO R3(ATmega328P)
An SD Card Module(Sry, I didn't get its specific name, but "MH-SD Card Module" was printed on it's back) with a 512MB SD card
I connect them with dupont lines

I tried your code and here is the result:
Type any character to start
Writing to test.txt...done.
error: opening test.txt for read failed
SdError: 0XD,0X7
(test.txt created but no data in it)

have you tried with another SD card?
are you sure it's a 512MB SD card ?

I don't have another SD card.

Well, To be precise, it is 479MB

it's a weird size...
the SdFat library offers an example to format the SD card, can you try that ?

SdFat/examples/SdFormatter at master · greiman/SdFat · GitHub

(I moved the discussion to the English speaking section)

I know why this is a "512MB" SD card.....

Card size: 0.50 GB (GB = 1E9 bytes)
Card size: 0.47 GiB (GiB = 2^30 bytes)
Card will be formated FAT16

Here is a part of the output of the code. It seems that the person who sold me SD card clearly mixed up 0.5G and 0.5MiB

Then the format started. But still returns error

Erasing
.error: erase failed
SD errorCode: SD_CARD_ERROR_READ_REG = 0x1c
SD errorData = 0x7

seems you have a bad SD card

Fine...
Anyway, thank you for your help!
Happy Chinese New Year