Problem writing filenames read from SD to TFT

Hi everybody, I have just recently downloaded the SdRaw and SdFat library and trying to use it.
I am using 5 inch tft & Mega with CTE shield. I have the example OpenNext.ino working with my SD
card. The problem I am having is I want to list the SD files on the TFT.
The example writes to Serial, how do I get the filenames into a character array (string) and write to
the TFT.



I saw your other post asking the same thing… And I couldn’t easily figure it out so I didn’t respond, but since you are now asking again, I came up with something that may help you in the right direction.

int maxfiles = 300;
int maxfilenamelength = 20;

#include <SPI.h>
#include <SdFat.h>

// SD chip select pin
const uint8_t chipSelect = 52;

// file system object
SdFat sd;

SdFile file;
void setup() {
  while (!Serial) {} // wait for Leonardo

  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
  char filenames[maxfiles][maxfilenamelength];
  int fileptr = 0;
  // open next file in root.  The volume working directory, vwd, is root
  while (file.openNext(sd.vwd(), O_READ)) {
    if (fileptr >= maxfiles) continue;
    file.getName(filenames[fileptr++], maxfilenamelength);
  for (int a = 0; a < fileptr; a++)
  Serial.print("Total files found :");
void loop() {}

You just need to decide how you will display them on your TFT screen, but all the filenames are stored in the array ‘filenames’.



Hi Graham, thanks for your reply, the solution you gave worked as expected.
I had tried "file.getName" before, but got "no member" error.
I didn't know it had 2 parameters. As a matter of interest where is (getName) defined as I couldn't find it.


PS thanks again for your help.

It is defined in FatFile.h, FatFileSFN.cpp and FatFileLFN.cpp.