sd-card doesn't work correctly

Hi!
I need to store a single line of data in csv file stored in sd-card module,but the data was written two times on two line at every upload the code on arduino.
what is the problem?Can you help me? thank you.
I uploaded a screen shot for the result on the serial monitor(1.png).
This is the code:

#include<SD.h>
#include<SPI.h>
File myfile;

void setup()
{

 Serial.begin(9600);
 
 Serial.print("Initializing card...");
 
 // declare default CS pin as OUTPUT
  pinMode(10, OUTPUT);
  
 if (!SD.begin(10)) {
   Serial.println("initialization of the SD card failed!");
   return;
 }
 Serial.println("initialization of the SDcard is done.");
 

 myfile = SD.open("textFile.txt", FILE_WRITE);
 
 
 if (myfile)
 {
   Serial.print("Writing to the text file...");
   myfile.println("Congratulations! You have successfully wrote on the text file.");
    
   myfile.close(); // close the file:
   Serial.println("done closing.");
 } else
 {
   // if the file didn't open, report an error:
   Serial.println("error opening the text file!");
 }
 
 // re-open the text file for reading:
 myfile = SD.open("textFile.txt");
 if (myfile)
 {
   Serial.println("textFile.txt:");
   
   // read all the text written on the file
   while (myfile.available()) 
   {
       Serial.write(myfile.read());
   }
   // close the file:
   myfile.close();
 } else 
 {
     // if the file didn't open, report an error:
   Serial.println("error opening the text file!");
 }
}

void loop()
{
}

FILE_WRITE mode appends data to the file. Every time your sketch runs, you will get another line.

had the same issue with mine. i went the easy way since I only have 1 piece of data to be saved.

add

SD.remove("textFile.txt");

before

myfile = SD.open("textFile.txt", FILE_WRITE);

this will delete the old file before writing the file. which will insure it only ever has the last set of data you saved.

wildbill:
FILE_WRITE mode appends data to the file. Every time your sketch runs, you will get another line.

But every running the sketch , two new lines were written in the csv file, not one line !

I can't see why, unless your sketch writes and then gets restarted somehow.

What does your serial output say?

wildbill:
I can't see why, unless your sketch writes and then gets restarted somehow.

What does your serial output say?

After every uploading the sketch two lines of data were written in the serial monitor as in the file
for example:
Congratulations! You have successfully wrote on the text file.
Congratulations! You have successfully wrote on the text file.

For example doesn't help. Please run the code, capture the serial output and post it.

wildbill:
For example doesn't help. Please run the code, capture the serial output and post it.

Ok.I'll upload a screen shot for the output on serial monitor.
You can find it in the attachment (1.png).

What do you get if you run it again?

wildbill:
What do you get if you run it again?

2.png in the attachment.

I still can't see the issue. I'd try dropping the section of code that reads the file and check the content on a PC. I suspect that something non obvious is wrong - library, hardware, wiring? Do you have another Arduino with an SD card. Might be worth trying a different SD card too.

wildbill:
I still can't see the issue. I'd try dropping the section of code that reads the file and check the content on a PC. I suspect that something non obvious is wrong - library, hardware, wiring? Do you have another Arduino with an SD card. Might be worth trying a different SD card too.

I tried, but the same problem appeared :frowning: