Appending datas to file instead of recreating the file (w/ SD.h library)

I’m using the SD.h library. With

myFile.printf(data);

I can write the string “data” to my sd card. But with this method, i recreate the file every time new. But instead of this, i want to append the file. Each time i call

myFile.printf(data);

The string “data” should be append the “old data string”, instead of deleting the old one.

#include "Arduino.h"
#include <SPI.h>
#include <SD.h>
File myFile;
uint16_t hex = 0;

void setup()
{
    if (!SD.begin()) {
        // problem
        return;
    }
}

void loop()
{
    myFile = SD.open("/test.txt", FILE_WRITE);

    if (myFile) {
        char data[50];
        sprintf(data, "%02X ", hex)
        myFile.printf(data);
        hex += 1;
    }
    else {
        // can't open file
    }
}

Let’s see the whole program that causes the problem. Seeing a single line in isolation is not good enough.

Added code in the startpost.

    myFile = SD.open("/test.txt", FILE_WRITE);Every time through loop() ?

Yes. Or also when i put this in a while true loop, instead of the void loop().

Shouldn't you be closing the file somewhere ?

Even if i close it, the problem still remains

#include "Arduino.h"
#include <SPI.h>
#include <SD.h>
File myFile;
uint16_t hex = 0;

void setup()
{
    if (!SD.begin()) {
        // problem
        return;
    }
}

void loop()
{
    myFile = SD.open("/test.txt", FILE_WRITE);

    if (myFile) {
        char data[50];
        sprintf(data, "%02X ", hex)
        myFile.printf(data);
        hex += 1;
        myFile.close();
    }
    else {
        // can't open file
    }
}

myFile.printf(data);Looks odd. Where did you get the library ?

Why not

myFile.println(data);

By the way, the code in your latest post will not compile.

Ok, the trick was:

myFile = SD.open("/test.txt", FILE_APPEND);]

instead of FILE_WRITE

Which Arduino board and version of the IDE are you using and where did you get the SD library ?