Pages: [1]   Go Down
Author Topic: SD.exists() has weird side effect  (Read 994 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I take the SD example "listfiles.ino" and run it on my Uno+Wireless SD shield, it lists the files on the SD card. So far so good.

If I add a single SD.exists call at the start, like this:

Code:
if(SD.exists("META.DAT"))    // <----- ADDED THIS
    {
        Serial.println("EXISTS");
    }
    else
    {
        Serial.println("DOESN'T EXIST");
    }
 
    root = SD.open("/");
   
    if(root)
    {
        Serial.println("root opened");
        printDirectory(root, 0);
    }

then the directory listing now returns nothing at all. The SD.open() still succeeds, but the call to dir.openNextFile() inside printDirectory() always fails.

It doesn't matter if the file I'm testing for actually exists on the SD card or not - SD.exists() returns the right result, but in either case dir.openNextFile() will fail.

This looks like I bug, unless I'm misusing it in some way...

Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13663
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And did you dive into the code what could cause it?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13663
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


had a quick look at the SD lib (in your arduino path) and the exists function calls the walkpath(...) function to walk the tree to see if it exists. What I saw it traverses the whole directory so some pointers might be at the end.

can you try rewindDirectory() ?

Which version of the Arduino are you using BTW?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1630
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

SD.open("/") doesn't really open root.  It just gives you a pointer to the already open internal root object.

The root file is positioned by SD.exists().  printDirectory() does not position dir so its behavior depends on the current position of dir.

Try calling rewindDirectory() before calling printDirectory() like this:
Code:
  root.rewindDirectory();
  printDirectory(root, 0);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's a Uno, with a wireless SD shield. Yes, rewindDir() fixes it, thanks all.
Logged

Pages: [1]   Go Up
Jump to: