Writing to SD seems to duplicate

I'm new at this and can't understand why this sketch duplicates the two lines I'm writing.

#include <SD.h>

// File to Write
char s[] = "TEST1.TXT"; 

File myFile;

void setup()
{
 // Open serial communications
  Serial.begin(9600);

  // Define sketch function
  Serial.print("Request to write file ");
  Serial.println(s);
  
  // Initialize SD card
  if (!initSDCard()) {
    return;
  }

  // Open file as output
  myFile = SD.open(s, FILE_WRITE);
 
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.println("Writing dummy data");
    myFile.println("testing 1, 2, 3.");
    myFile.println("testing 4, 5, 6.");
    // close the file:
    myFile.close();
    Serial.print("File ");
    Serial.print(s);
    Serial.println(" has been created");
  } else {
    // if the file didn't open, print an error:
    Serial.print("File ");
    Serial.print(s);
    Serial.println(" could not be created"); 
  }
}

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

// Initialize the SD card
int initSDCard() {
  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("SD card initialization failed!");
    return false;
  }
  return true;
}

I'm very new to this so please forgive any glaring error! My conclusion that it duplicates the two lines is that the filesize is 72 rather than 36 and a little filedump sketch shows the files as if I'd written the two lines a second time.

Thank you

I don't see anything in the code you posted that would explain the issue you are seeing. So, I have to wonder if the issue is really with the writing to the file, or with the code you didn't post that reads from the file.

Fair enough. Here is the sketch that reads the file and the output it produces:

#include <SD.h>

// File to Read
char s[] = "TEST1.TXT"; 

File myFile;

void setup()
{
 // Open serial communications
  Serial.begin(9600);

  // Define sketch function
  Serial.print(F("Request to read file "));
  Serial.println(s);
  
  // Initialize SD card
  if (!initSDCard()) {
    return;
  }

  // Open the file for reading:
  myFile = SD.open(s);
  if (myFile) {
    Serial.println(F("Printing content of file"));
   
    // 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(F("Unable to open the file as input"));
  }
}

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

// Initialize the SD card
int initSDCard() {
  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println(F("SD card initialization failed!"));
    return false;
  }
  return true;
}

Request to read file TEST1.TXT
Printing content of file
testing 1, 2, 3.
testing 4, 5, 6.
testing 1, 2, 3.
testing 4, 5, 6.

Could it be that the writing sketch somehow gets run twice (sure I didn't do that explicitly) and appends?

impresario:
Could it be that the writing sketch somehow gets run twice (sure I didn't do that explicitly) and appends?

Did you perhaps upload the sketch, which started it running, and then open the serial monitor, resetting the Arduino, and starting the sketch running again?

You are opening the file in append mode, so doing what I describe would match with the results you see.

Thank you for your help!

My take away is that I should probably test for the existence of a file before writing it, unless I intend on appending.

Also, I'm in the habit of loading a sketch and once it's loaded, start the serial monitor. I didn't realize starting the monitor resets the arduino.