# How to properly open and close files “inside” a member function? [closed]

I am using Arduino Uno and the Ethernet Shield with a 2GB SD Card. I have the following working code intended to be used to write data on the SD Card (note: the SD Library is properly initialized and I just hide the reated code in order to keep things smaller):

class Logger {
private:
File myFile;
char *myFilename;

public:
Logger (char *myFilename = "text.txt") : myFilename(myFilename) {
myFile = SD.open(myFilename, FILE_WRITE);
}

void writeAction () {
if (myFile) {
myFile.print("Sample text.");
myFile.close();
} else {
Serial.print("Error opening the file ");
}
}
};

void setup() {
Logger logger;

...

logger.writeAction();
}


However, if I change a bit the above code as made in the following one, it will not work as expected: the "Sample text." is not written / saved on the SD card.

class Logger {
private:
File myFile;
char *myFilename;

public:
Logger (char *myFilename = "text.txt") : myFilename(myFilename) {
// Note: Here is the change. File opening statements are moved
// "inside" the writeAction function.
}

void writeAction () {
myFile = SD.open(myFilename, FILE_WRITE);

if (myFile) {
myFile.print("Sample text.");
myFile.close();
} else {
Serial.print("Error opening the file ");
}
}
};

void setup() {
Logger logger;

...

logger.writeAction();
}


Why does it happen? How can I make the myFile to be properly opened and closed "inside" the writeAction function?

• I don't remember my C++ file handling functions off the top of my head, but are you sure your settings are correct to clobber and overwrite text.txt if it already exits? – The Photon Feb 26 '13 at 0:14
• I can't follow why your constructor is written that way but I wouldn't expect the global myFilename to be set. Try giving it a different name in the constructor and copy it to the global. – PeterJ Feb 26 '13 at 0:51
• smells like a race condition, what happens if you put delay(1000); after myFile = SD.open(myFilename, FILE_WRITE); in your second code? – vicatcu Feb 26 '13 at 4:57
• I don't know C++ at all well (Ada compilers tell you exactly what's wrong far more of the time!) but I am suspicious that myFile is left uninitialised by the second constructor : can you initialise it to a known "no file" value? – user_1818839 Feb 26 '13 at 9:57
• How is this a electrical engineering problem? This looks like a pure high level software problem. – Olin Lathrop Mar 4 '13 at 22:05

A common (and difficult to isolate) problem that sometimes happen in embedded systems development is that something that worked previously stops working after some seemingly innocuous change to the software. There are a great many reasons why this can happen, but the two most common are:

• OK, maybe I understood what you mean and I am thinking my case is related to a "Undocumented (or Unrealized) Timing Dependency / Assumption". So, since I would like to "really understand what's going on and adjust things accordingly", where I can read the source code of the Arduino SD library? – Backo Mar 4 '13 at 22:22