Need advice with saving files

Hi everybody!
I need advice, I'm trying make code which is dividing data in to 37 MB files. The data comes from GPS.
I did code, when I turn on board it creates file and starts save in to SD card, I turn off and turn again on it creates new file and starts save data, that's ok, I deal with this. But I want make code which is saves files with volume 37 MB during saving process also, example: gpslog00, gpslog01 and so on. I think you got idea.
This is what I did in setup():
void setup()
{
Serial.begin(19200);
flag = 0;
card.init();
// initialize a FAT16 volume
Fat16::init(&card);

int i;
for(i=0; i<100; i++)
{
name[6] = '0'+i/10;
name[7] = '0'+i%10;
if(!myFile.open(name, O_EXCL))
{
break;
}
}
myFile.open(name, O_CREAT | O_APPEND | O_WRITE);
MsTimer2::set(450,stopGps);//Set Timer2 Interrupt every 900ms(at 8MHz)
}

and I couldn't figure out how make do dividing files during process of saving.

void loop()
{
if(Serial.available())
{
recv = Serial.read();
if(recv == 0x0A)
{
recvBuf = "";
}
recvBuf += recv;
nameIndex = recvBuf.indexOf(HeadMessage);
if(nameIndex != -1)//if there has HeadMessage there will not return -1,so we can know is ther a new message
{
recvBuf = "";
flag = 1;
}
if(flag == 1)
{
Gpsflag = 1;
MsTimer2::start();
myFile.print(HeadMessage);//wo miss the headmessage up there,so we add the head message before we add the next
while(Gpsflag)//Get one Packet message during the 900ms,cause the GPS send the message every 1 second
{
while(Serial.available())
{
myFile.print((char)Serial.read());
}
}
flag = 0;//clear the flag,waiting for the next Packet
MsTimer2::stop();
myFile.close();//Close and Save
myFile.open(name,O_APPEND | O_WRITE);//allow to Write
Serial.flush();//Clrean the Serial Buff
}
}
}

Please help, I will be really appreciate!
BR,
kea2288 :slight_smile:

You should just be able to close the old file, generate a new name, and open the new file.

Just keep a record of the number of bytes written to your current file (there may be a function already to see how big the file is) and if it exceeds your threshold just increase your file "number" and make a new name from it. You already seem to be opening and closing your file many times. This could shorten the lifetime of your SD card. You would be better off only closing the file when it has reached your size limit, or periodically to ensure data is flushed to the card.

At the moment it looks like you're doing:

  1. Open file.
  2. Wait for packet.
  3. Write data
  4. Close file
  5. Open file
  6. Goto 2

It seems a little odd.

If you have to have constant opening and closing, why not just wrap the writing in an open/close pair? thus:

  1. Wait for packet
  2. Open file
  3. Write data
  4. Close file
  5. Goto 1

Much simpler.

With size checking:

  1. Wait for packet
  2. Check size of current file.
    2a. If > max increase file number
  3. Generate file name
  4. Open file
  5. Write data
  6. Close file
  7. Goto 1

Hi Majenko!
Thank you very much! Your advice really useful.
Sorry for late feedback.

Thank you,

BR,
kea2288