[SOLVED] How can I clear the File variable?

My project has a remote unit that radio's data to the base unit, which then needs to save the data to an SD Card file.

When the remote unit turns on, the base needs to check to see if the file has already been opened, or if it needs to create a new file instead.

I'm concerned that the File variable might already have random data in it at startup, causing a boolean check to report the file open even when no file exists, unless I clear the File variable at startup. So how can I clear it?

File f = 0;

gives an error message, as does

File f= false;

I tried

File f = SD.open("");

But that makes f true! ... even though no file was created.

f.close()

works, but what if there was junk in f already that close() ignored because it wasn't a real file, allowing f to remain true?

How can I safely clear f during startup to be sure it will read false until a file is actually opened?

Use exists().

When a File object is created, it contains a pointer. That pointer is NULL if you have:

File f;

The if(f) test actually tests the internal pointer, which gets valued only when you actually open a file (and cleared when you close it).

So, you don't actually have to do anything. The class has done all the work.

aarg: Use exists().

In my case, the filename is time/date (two digits each for month/day/hour/minute) making it always something new and different, so "exists()" wouldn't know what name to look for.

PaulS: When a File object is created, it contains a pointer. That pointer is NULL.... So, you don't actually have to do anything. The class has done all the work.

What a clear and complete explanation you gave! Problem solved!