Writing a function for writing to SD file

Hi

I'm using a sketch that uses a little function (if that is the correct term) called to format the time with leading zeros.

I'd like to do the same thing, but for writing to the SD card - but I cannot write a similar function using dataFile.print() as "dataFile" is not defined until the loop.

Hope that makes some sort of sense.

Thanks very much.

#include <SD.h>
#include <Ethernet.h>
#include <SPI.h>
#include <Time.h>

const int chipSelect = 4;

void setup(void)
{
  setTime(12,0,0,23,3,12); 
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
}

void digitalClockDisplay(){
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.println(year());
}
void printDigits(int digits){
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void loop()
{

  digitalClockDisplay();    //prints the date/time to serial as per the function

  File dataFile = SD.open("test.txt", FILE_WRITE);

  dataFile.println("what time is it??????");   //I want to write the time to the SD file with leading zeros

  dataFile.close();

  delay(1000);
}

dataFile is defined in loop(), though, so you can pass it to a function.

void someFun(File &theForToWriteTo, int whatever)
{
   someFileToWriteTo.print(whatever);
}

void loop()
{
   File dataFile = SD.open("test.txt", FILE_WRITE);
   someFun(dataFile, 42);
}

Thanks PaulS, you're a star.