Writing files to SD

hey guys, i have a camera and sd breakout connected to my arduino uno. basically i’m confused on how to create a new file every time i reset/upload to the arduino. right now it creates “test.txt” and just writes every following session onto the same file. i want a new file for every session, so “test01.txt, test02.txt, test03.txt, etc.”

here’s my code:

#include <NewSoftSerial.h>

#include <JPEGCamera.h>

#include <FatStructs.h>
#include <Sd2Card.h>
#include <Sd2PinMap.h>
#include <SdFat.h>
#include <SdFatmainpage.h>
#include <SdFatUtil.h>
#include <SdInfo.h>

#include <MemoryCard.h>
#include <MemoryCardDevice.h>


/*
JPEG Camera Example Sketch
The sketch will take a picture on the JPEG Serial Camera and store the jpeg to an SD card
on an SD Shield
Written by Ryan Owens
SparkFun Electronics

Hardware Notes:
This sketch assumes the arduino has the microSD shield from SparkFun attached.
The camera Rx/Tx should be attached to pins 2 and 3.
IMPORTANT: The JPEG camera requires a TTL level shifter between the camera output
and the arduino. Bypassing this may damage the Arduino pins.
*/

//This example requires the MemoryCard, SdFat, JPEGCamera and NewSoftSerial libraries
#include <MemoryCard.h>
#include <SdFat.h>
#include <JPEGCamera.h>
#include <NewSoftSerial.h>

//Create an instance of the camera
JPEGCamera camera;

//Create a character array to store the cameras response to commands
char response[32];
//Count is used to store the number of characters in the response string.
unsigned int count=0;
//Size will be set to the size of the jpeg image.
int size=0;
//This will keep track of the data address being read from the camera
int address=0;
//eof is a flag for the sketch to determine when the end of a file is detected
//while reading the file data from the camera.
int eof=0;

void setup()
{
    //Setup the camera, serial port and memory card
    camera.begin();
    Serial.begin(9600);
    MemoryCard.begin();

    //Reset the camera
    count=camera.reset(response);
    delay(3000);

    //Take a picture
    count=camera.takePicture(response);
    //Print the response to the 'TAKE_PICTURE' command.
    Serial.write(response);
    Serial.write(count);
    Serial.println();

    //Get the size of the picture
    count = camera.getSize(response, &size);
    //Print the size
    Serial.print("Size: ");
    Serial.println(size);

    //Create a file called 'test.txt' on the SD card.
    //NOTE: The memoryCard libary can only create text files.
    //The file has to be renamed to .jpg when copied to a computer.
    MemoryCard.open("/test.txt", true);
    //Starting at address 0, keep reading data until we've read 'size' data.
    while(address < size)
    {
        //Read the data starting at the current address.
        count=camera.readData(response, address);
        //Store all of the data that we read to the SD card
        for(int i=0; i<count; i++){
            //Check the response for the eof indicator (0xFF, 0xD9). If we find it, set the eof flag
            if((response[i] == (char)0xD9) && (response[i-1]==(char)0xFF))eof=1;
            //Save the data to the SD card
            MemoryCard.file.print(response[i], BYTE);
            //If we found the eof character, get out of this loop and stop reading data
            if(eof==1)break;
        }
        //Increment the current address by the number of bytes we read
        address+=count;
        //Make sure we stop reading data if the eof flag is set.
        if(eof==1)break;
    }
    //Close the file
     MemoryCard.close();
     Serial.print("Done.");
}

void loop()
{
  

}

Use the eeprom on a arduino to store the last used file name.

See http://www.arduino.cc/en/Reference/EEPROM

So if you store a 11 in the eeprom then with your next session you know that test11.txt is already used and that it should use the next available name test12.txt. Values stored in the eeprom will retain its data even if the device is shut down. As long as it is not overwritten then you can still use the data every time you use the eeprom.