If you are running the sketch several times you may want to use the O_APPEND flag. Each time the sketch runs new data will be appended to the file. This is how FILE_WRITE works, it appends to the file.
Here is an example.
#include <SD.h>
File file;
//------------------------
void setup() {
Serial.begin(9600);
if (!SD.begin()) {
Serial.println("begin failed");
return;
}
file = SD.open("TEST_SD.TXT", O_CREAT | O_APPEND | O_WRITE);
file.println("Hello");
// for test - better to close the file when finished
file.flush();
}
//--------------------------
void loop() {}
If you want to delete all data each time a sketch runs use the O_TRUNC flag with open to truncate the file to zero length:
file = SD.open("TEST_SD.TXT", O_CREAT | O_TRUNC |O_WRITE);
Here are all of the open flag values for SdFat. I am not sure they will all work with the Arduino SD wrapper. You can download SdFat here
http://code.google.com/p/sdfatlib/.
Open flag values are constructed by a bit-wise-inclusive OR of flags from the following list.
O_READ - Open for reading.
O_RDONLY - Same as O_READ.
O_WRITE - Open for writing.
O_WRONLY - Same as O_WRITE.
O_RDWR - Open for reading and writing.
O_APPEND - If set, the file offset shall be set to the end of the file prior to each write.
O_CREAT - If the file exists, this flag has no effect except as noted under O_EXCL below. Otherwise, the file shall be created
O_EXCL - If O_CREAT and O_EXCL are set, open() shall fail if the file exists.
O_SYNC - Call sync() after each write. This flag should not be used with write(uint8_t), write_P(PGM_P), writeln_P(PGM_P), or the Arduino Print class. These functions do character at a time writes so sync() will be called after each byte.
O_TRUNC - If the file exists and is a regular file, and the file is successfully opened and is not read only, its length shall be truncated to 0.