Few odd issues.

A) I can't seem to be able to create a new file. This is intermittent. Sometimes the new file is created.
B) I can't write to an empty file using print(). This is NOT intermittent. Just can't do it.
C) If there is any data in the file? Everything seems to work fine.

This came up working on a library file for event logging. So I pulled out the offending part and set it up in its own .ino file to demonstrate the issue. Can anyone have a look?

#include "SD.h"

#define SD_CS     4
#define MAX_BYTES 200

char  dataBuff[MAX_BYTES+1];
int   i;

// These guys are here to emulate the stuff from the class this stuff was pulled from.
char mPath[] = "/logTest.txt";
bool getLogging() { return true; }
int mSeries = 1;

void setup() {

   i = 0;
   if (!SD.begin(SD_CS)) {
      Serial.println("NO SD CARD!");
   Serial.println("Logging's up and running.");

bool addEvent(char* eventTxt) {
   File  logFile;
   bool  success;
   int   gSec = 99; // Also from the class.
   success = false;                                   // Not been a success yet.
   if (getLogging()) {                                // If we -are- logging..
      logFile = SD.open(mPath, FILE_WRITE);           // Lets try to open/create the logfile.
      if (logFile) {                                  // If we had success..

         logFile.print(mSeries);logFile.print('\t');  // First we print out the series number.
         logFile.print(gSec);logFile.print('\t');     // Next the number of seconds in this series.
         logFile.println(eventTxt);                   // And whatever their data ends up being.
         logFile.close();                             // Close the file. (Always leave files closed!)
         success = true;
   return success;

void loop() {

  char   inChar;
  if (Serial.available()) { 
      inChar = Serial.read();                       
      if (inChar=='\n') {
         dataBuff[i] = '\0';
         if (addEvent(dataBuff)) {
            Serial.print(dataBuff);Serial.println(", added to event log.");
         } else {
            Serial.println("No data added.");
         i = 0;
      } else if (i<MAX_BYTES) {
         dataBuff[i] = inChar;
There you go. Change the chip select to match your system, and it should compile and run.

Thanks millions!

-jim lee