Go Down

Topic: SD.exists() has weird side effect (Read 1 time) previous topic - next topic

blowback

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: [Select]

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...


robtillaart

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

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

robtillaart


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?
Rob Tillaart

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

fat16lib

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: [Select]

  root.rewindDirectory();
  printDirectory(root, 0);

blowback

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

Go Up