Mzim
June 30, 2017, 5:35pm
1
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.
Mzim
June 30, 2017, 5:55pm
3
Added code in the startpost.
myFile = SD.open("/test.txt", FILE_WRITE);
Every time through loop() ?
Mzim
June 30, 2017, 6:28pm
5
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 ?
Mzim
June 30, 2017, 6:47pm
7
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.
Mzim
June 30, 2017, 8:00pm
9
Ok, the trick was:
myFile = SD.open("/test.txt", FILE_APPEND);]
instead of FILE_WRITE
1 Like
Which Arduino board and version of the IDE are you using and where did you get the SD library ?