Checking if a file has been opened successfully in SD library

jrveale
Posts: 9
Joined: Mon Apr 01, 2019 9:30 am

Checking if a file has been opened successfully in SD library

Postby jrveale » Wed Feb 24, 2021 5:40 pm

Hi,

I've always thought that the way to check that an SD.open() was succesful was as follows.

Code: Select all

File myFile = SD.open("/path/to/file.txt", FILE_WRITE);
if(!myFile)
{
    // Oh no, myFile failed to open successfully
}
This is corroborated by all the examples I find online, and by the examples within the SD library. The Arduino documentation states that a File will evaluate as false if the open hasn't been successful - so I'd expect the above to work.

However, if I try to test this by removing my microSD card between writes, the above code doesn't pick up the error at all. I get a serial print of the error from vfs_api.cpp letting me know that the open has failed, but my if(!myFile) test doesn't work and so my code doesn't spot the error at all.

So it seems the examples are wrong? (Assuming others can replicate this issue.)

Is this a bug, or a specific case in which the above check isn't meant to work?

Does anyone have a method of checking if file opening has been successful?

Thanks in advance for any insight!

jrveale
Posts: 9
Joined: Mon Apr 01, 2019 9:30 am

Re: Checking if a file has been opened successfully in SD library

Postby jrveale » Fri Feb 26, 2021 3:39 pm

Update: See this issue on github. Sounds like this might be a bug.

Who is online

Users browsing this forum: No registered users and 46 guests