Go Down

Topic: SdFat issue with Filenames (Read 374 times) previous topic - next topic

gcharles

Aug 19, 2019, 09:36 pm Last Edit: Aug 19, 2019, 09:42 pm by gcharles
Hello I have below code that works perfectly on SD library , it prints on Serial the filenames of the files in root of the SD

But when I changed the code to use SdFat I have issue because instead of the filenames I am getting back

Serial output when using SdFat library - always use getName

File 1 Name is - use getName()
File 2 Name is - use getName()
File 3 Name is - use getName()

Serial output when using SD Library - Works Fine

File 1 Name is - myf1.NC
File 2 Name is - myf2.NC
File 3 Name is - myf3.NC


Complete codes are attached

Get filename function is below

String getFileName(byte i) {
  /*
    Returns a filename.
    if i = 1 it returns the first file
    if i = 2 it returns the second file name
    if i = 3 ... see what I did here?
  */
  byte x = 0;
  String result;
 File root = sd.open("/");
 
  while (result == "") {
     File entry =  root.openNextFile();
    if (!entry) {
      // noting
    } else {
      if (!entry.isDirectory()) {
        x++;
        if (x == i) result = entry.name();
      }
      entry.close();
    }
  }
  root.close();
  return result;
}

pert

Here you can see the definition of the name() function in the SDFat library:
https://github.com/greiman/SdFat/blob/1.1.0/src/FatLib/ArduinoFiles.h#L164-L171
Code: [Select]
  /** No longer implemented due to Long File Names.
   *
   * Use getName(char* name, size_t size).
   * \return a pointer to replacement suggestion.
   */
  const char* name() const {
    return "use getName()";
  }

You can see that the name() function has been discontinued and the library is trying to tell you that you need to use the getName() function instead.

gcharles

Hello Thanks for the point but in the same file there is no getName() function that can be used

Thanks again

pert

Here's the function prototype:
https://github.com/greiman/SdFat/blob/1.1.0/src/FatLib/FatFile.h#L372-L382
Code: [Select]
  /**
   * Get a file's name followed by a zero byte.
   *
   * \param[out] name An array of characters for the file's name.
   * \param[in] size The size of the array in bytes. The array
   *             must be at least 13 bytes long.  The file's name will be
   *             truncated if the file's name is too long.
   * \return The value true, is returned for success and
   * the value false, is returned for failure.
   */
  bool getName(char* name, size_t size);




gcharles

I have to check that but that my function have to completely re write

Thanks

Go Up