Checking if a file has been opened successfully in SD library
Posted: 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.
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!
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
}
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!