I am writing a wav file to an SD card on a ESP32 and that requires that every time I append data to the the end of the file I modify the header to set the right size in some fields.
I cannot find how to replace a value inside a file, I tried to write the new values with the seek function but unless I'm missing something looks like this deletes everything after the new value added.
I do not want to create a new file and to delete the old one as I will storing potentially large wav files and looks like wasting resources.
Is there a way to overwrite one value inside a file. I'm using SDlib but I'm open to others too.
Here the code I use to create the file and to update it:
#include "WavFileWriter.h"
void WavFileWriter::createFile(char const* filename, WavFileHeader* wavFileHeader)
{
byte headerBuffer[44];
wavFileHeader->getHeaderData(headerBuffer);
File file = SD.open(filename, FILE_WRITE);
file.write(headerBuffer, 44);
file.close();
}
void WavFileWriter::appendData(char const* filename, WavFileHeader* wavFileHeader, byte* data, int dataLength)
{
File file = SD.open(filename, FILE_WRITE);
file.write(data, dataLength);
byte headerBuffer[44];
wavFileHeader->setFileSize(file.size());
wavFileHeader->getHeaderData(headerBuffer);
file.seek(0);
file.write(headerBuffer, 44);
file.close();
}
After calling appendData I end up with a file with just the header size (with the right header for a file with data but the data is gone).
2 Answers 2
The FILE_WRITE
in SD library is 'append' (for historical reasons)
#define FILE_WRITE (O_READ | O_WRITE | O_CREAT | O_APPEND)
so use
File file = SD.open(filename, O_WRITE);
-
Looks like the ESP32 uses a different library and the definitions are different: #define FILE_READ "r" #define FILE_WRITE "w" #define FILE_APPEND "a"Ignacio Soler Garcia– Ignacio Soler Garcia2020年05月25日 16:29:31 +00:00Commented May 25, 2020 at 16:29
-
Should I use SPIFFS instead of SD?Ignacio Soler Garcia– Ignacio Soler Garcia2020年05月25日 17:03:08 +00:00Commented May 25, 2020 at 17:03
SD.seek() is the API you're looking for. It allow you to position the read/write pointer within a file. In standard C++ the fseek() API is used.
-
-
I added the code so you can see if this is the expected usage.Ignacio Soler Garcia– Ignacio Soler Garcia2020年05月25日 12:11:02 +00:00Commented May 25, 2020 at 12:11
-
It looks like you're not seek'ing to the end of the file when you attempt to append data. Try: file.seek(file.size()) between the SD.open() and file.write() in the WavFileWriter::appendData() method.Jeff Wahaus– Jeff Wahaus2020年05月26日 14:09:13 +00:00Commented May 26, 2020 at 14:09
wav
format?