arduino write double times, but i only code only write 1 sentense

Is there any solution? I read my file that double my sentence
Example
reality
123
123
asd
asd

my expectation
123
asd

#include <SD.h>
const int chipSelect = 10;
File myfile;
String alamatFile = "data.txt";

void setup()
{
 
  Serial.begin(9600);//SetBaud rate 9600
  pinMode(10, OUTPUT);// arduino-SD CS pin 10
  SD.begin(10);
 
  myfile = SD.open(alamatFile, FILE_WRITE);
 
    myfile.println("demetrioAsdsdsdadsd");
      myfile.close(); 
   
 
  // re-open the text file for reading:
  myfile = SD.open(alamatFile);

    while (myfile.available()) 
    {
      Serial.write(myfile.read());
    }
    // close the file:
    myfile.close();
 
}

void loop()
{
}

Your code writes "demetrioAsdsdsdadsd" to the file.
Where do you get "123" and "asd" from?

Pete

When you open the serial monitor, it resets the program and it writes "demetrioAsdsdsdadsd" a second time.

One way to see this is download the example program, pull the card, and read it in your computer before opening the serial monitor.

If you want to eliminate writing in setup before the monitor is opened, you can add these lines after Serial.begin() to wait in the while loop until you enter a character. Then you will read "demetrioAsdsdsdadsd" only once.

Serial.println("Type any character to start");
  while (Serial.read() <= 0) {}

What you are seeing is typically a problem with SD demo type programs.

If you want to eliminate writing in setup before the monitor is opened, you can add these lines after Serial.begin() to wait in the while loop until you enter a character. Then you will read "demetrioAsdsdsdadsd" only once.

Or, you can change the mode used to open the file, to include the flag to truncate the file each time it is opened.