I encounter some problem using file.seek(0) with SD library (try both 1.2.0 and 1.2.3)
My idea is to append some data from sensor at the begining of my sd file (instead of at the end as usually) to allow easier reading after (just have to count for 10 first lines from start that will represent the more recent data point).
I record data1, then data2 then data 3 and I want the file to be as follow :
data3
data2
data1
I can't get it to work.
I will have :
data1
data2
data3
of if I delete O_APPEND, I get just
data3
Any advice ?
Thank you
I made a small exemple based on the build-in tutorial SD/Datalogger :
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
}
void loop() {
// make a string for assembling the data to log:
String dataString = "";
delay(2000);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", O_READ | O_WRITE | O_CREAT | O_APPEND);
// if the file is available, write to it:
if (dataFile) {
for(int i=1;i<=3;i++){
dataFile.seek(0);
dataFile.println(i);
// print to the serial port too:
Serial.println(i);
}
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}