Need a little help with the new SdFat beta

Hi All…

I think this code should work, but it does not. I am simply checking to see if a file exists, and whether the file does exist or not, SdFat reports that it does not exist. here is the sketch, thanks!

#include <SdFat.h>

// SD chip select pin
//const uint8_t chipSelect = SS_PIN;
const uint8_t chipSelect = 31;

// file system object
SdFat sd;

//------------------------------------------------------------------------------
void setup() {

  Serial.begin(9600);

  // pstr() stores strings in flash to save RAM
  Serial.println("any key to start");
  while (!Serial.available());

  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();

  SdFile tmpSdFile;
    
  if(tmpSdFile.exists("TESTFILE.CSV"))    
  {
    Serial.println("FILE EXISTS");
  }
  else
  {
    Serial.println("FILE NOT FOUND");
  }

}      


//------------------------------------------------------------------------------
void loop() {}

You want

 if(sd.exists("TESTFILE.CSV"))    
  {
    Serial.println("FILE EXISTS");
  }
  else
  {
    Serial.println("FILE NOT FOUND");
  }

The form you are using tests for a file in a directory file .

 if (dirFile.exists("TESTFILE.CSV"))

You must open the directory file, dirFile, first to use this form.

sd.exists() test for the file in the current working directory which is root in your case.

Since exists() is a member of the SDFile class, it seems strange that it is not a static method, since it does absolutely nothing with/to/about the instance that needs to be created to use the function.

Comments?

SdFile::exists() searches the instance, which must be an open directory, for the file.

SdFat::exists() seacrches the current working directory for the file. The current working directory is a private member of the SdFat instance.

To bad both classes have have a member functions named exist().

SdFile::exists() searches the instance, which must be an open directory

So an SdFile instance is a directory. Well, that's certainly a good choice of class names. Or, I am missing something.

SdFile is a file.

In FAT a directory is just a file with an extra attribute bit.

So you open and read directories just like data files.

You can do this with Arduino's SD.h wrapper for SdFat.

Thank you!

SdFile is a file.

In FAT a directory is just a file with an extra attribute bit.

So you open and read directories just like data files.

Thank you for the explanation. That was not at all clear from the header or source files, with respect to what exists() in the SdFile class does.