Hi everyone,
I would like to create a queue on a file on SD card. I want to store struct data in the queue. To make it easy I write data in the form of bytes.
I managed to write the data on the file using the following function:
void enqueue(const struct_data theData, bool debug) {
String queueFileName = "/QUEUE.TXT";
if (!SD.exists(queueFileName)) {
File queueFile = SD.open(queueFileName, FILE_WRITE);
queueFile.close();
}
File queueFile = SD.open(queueFileName, FILE_APPEND);
if (queueFile) {
queueFile.write((const uint8_t *)&theData, sizeof(struct_data));
}
else {
if (debug) Serial.println("Can't open queue");
}
queueFile.close();
}
And I can read the data in the loop function as follow:
String queueFileName = "/QUEUE.TXT";
File queueFile = SD.open(queueFileName, FILE_READ);
while (queueFile.available() > 0) {
struct_data buffer;
queueFile.read((uint8_t *)&buffer, sizeof(struct_data));
printData(buffer);
Serial.write((const uint8_t *)&buffer, sizeof(struct_data));
Serial.println();
}
queueFile.close();
However, when I write almost the same lines inside a specific dequeue function, it doesn't work anymore. It feels like some bytes are "corrupted". I don't have any error but the format of the struct isn't respected and I can't get back the values of the several fiels.
bool dequeue(struct_data theData, bool debug) {
String queueFileName = "/QUEUE.TXT";
// check if the queue file exists
if (!SD.exists(queueFileName)) {
if (debug) Serial.println("No queue available!");
return false;
}
File queueFile = SD.open(queueFileName, FILE_READ);
if (!queueFile) {
if (debug) Serial.println("Failed to open the queue file!");
return false;
}
// check if the queue is empty
if (queueFile.available()<=0) {
if (debug) Serial.println("Queue is empty!");
return false;
}
// read the first element of the queue
queueFile.read((uint8_t *)&theData, sizeof(struct_data));
String tempFileName = "/TEMP.TXT";
// ensure the temp file does not already exist, and if it does, remove it
if (SD.exists(tempFileName)) {
SD.remove(tempFileName);
}
File tempFile = SD.open(tempFileName, FILE_WRITE);
if (!tempFile) {
if (debug) Serial.println("Failed to create the temporary file!");
queueFile.close();
return false;
}
// transfer remaining elements to the temp file
while (queueFile.available() > 0) {
struct_data buffer;
queueFile.read((uint8_t *)&buffer, sizeof(struct_data));
tempFile.write((const uint8_t *)&buffer, sizeof(struct_data));
}
queueFile.close();
tempFile.close();
// remove the old queue file
SD.remove(queueFileName);
char c_tempFileName[tempFileName.length() + 1];
char c_queueFileName[queueFileName.length() + 1];
memcpy(c_tempFileName, tempFileName.c_str(), tempFileName.length() + 1);
memcpy(c_queueFileName, queueFileName.c_str(), queueFileName.length() + 1);
if (!SD.rename(c_tempFileName, c_queueFileName) && debug) {
Serial.println("Failed to rename the temporary file!");
return false;
}
return true;
}
void enqueue(const struct_data theData, bool debug) {
String queueFileName = "/QUEUE.TXT";
if (!SD.exists(queueFileName)) {
File queueFile = SD.open(queueFileName, FILE_WRITE);
queueFile.close();
}
File queueFile = SD.open(queueFileName, FILE_APPEND);
if (queueFile) {
queueFile.write((const uint8_t *)&theData, sizeof(struct_data));
}
else {
if (debug) Serial.println("Can't open queue");
}
queueFile.close();
}
Does someone understand why it doesn't work?
Thanks in advance