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).
wav
format? – jsotola May 24 '20 at 15:49