Reading and Writing into SD card

I am working on a project. I need to read a .wav file from SD card, and convert it into base64 text form. I am reading the .wav file 30bytes at a time, and converting it into base64 text. I wish to save this text in the same SD card. So I have written the following code. But the problem is, whenever I close a file, it resets its file pointer to the beginning. So the program is essentially reading the same data again and again. Can you suggest some changes, or strategies. Maybe some other header file.

All help is highly appreciated.

#include <rBase64.h>
#include<SD.h>

#define SD_CSPin 4

short int i=0;
char ch[30];
char dh[40];

File sd,td;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
 Serial.println("loading... SD card");
  if (!SD.begin(SD_CSPin)) {
            Serial.println("An Error has occurred while mounting SD");
  }
  while (!SD.begin(SD_CSPin)) {
            Serial.print(".");
            delay(500);
  }

while(1)
{
i=0;
sd=SD.open("1.wav");
if(!sd.available())
{
  Serial.print("Conversion Complete");
  break;
}
for(i;i<30;i++)
{
  ch[i]=sd.read();
}
strcpy(dh,rbase64.encode(ch));
Serial.println(ch);
sd.close();
td=SD.open("tmt.txt",FILE_WRITE);
td.print(ch);
td.close();

}


  
}

void loop() {
  // put your main code here, to run repeatedly:

}

As a quick fix, you may be able to use the seek() function on the source file each time you open it. You know that you read in 30 bytes, so the second time around the loop do a seek(30) before reading. Then the third time do a seek(60) etc.

Indeed, td.seek(pos) to put the file pointer at a location and pos =td.position(); to remember where you left before closing the file.

1 Like

sd.seek() for reading the source file. Can't recall off the top of my head if the print() function will append or overwrite....