Go Down

Topic: Need a little help with the new SdFat beta (Read 4539 times) previous topic - next topic

skyjumper

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!

Code: [Select]
#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() {}

fat16lib

You want
Code: [Select]
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 .
Code: [Select]
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.

PaulS

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?
The art of getting good answers lies in asking good questions.

fat16lib

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

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

fat16lib

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.


PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

Go Up