Understanding Pointers and File Naming: SD Card Help?

I'd like to be able to scroll through some files on an SD card via an LCD. I only want to print certain file types to the LCD screen so my thought was to write a bit of code that nabbed a character array corresponding to the filename (in 8.3 format) and read the last three characters. If those last three characters were "csv" then I would print.

However, after hours of reading forum questions and trying to read through the SD.h and SD.cpp files, I'm no closer to my goal. Right now, I can return a pointer to the actual file name via the function name(). I want to be able to access the actual file name and store it as a string or an array of characters.

Alternatively, can someone help me understand the File class? It looks like openNextFile() returns File(f, name) where "name" looks like it is the character array that I want. How do I access it?

File root;
File entry;
char fileName[12];
char* entryName;
setup(){
  root = SD.open("/");
  while(1){
    entry = root.openNextFile();
    entryName = entry.name();
    //entryName.toCharArray(fileName, sizeof(fileName));  //this is nonsense
    if (! entry) break;
    Serial.println(entryName); //prints out the name as expected
    //let's pretend like I can get my character array ... then:
    if (fileName[10] == 'c' && fileName[11] == 's' && fileName[12] == 'v'){
      lcd.println(entryName); //haven't tried this yet and obviously I'd have to setup my LCD first
    }
  }
}

Or a more elegant solution if anyone cares to share one!

You could loop through the file name checking characters until you hit a period. Then look at the next three characters. Most filenames only have the one period.

Delta_G:
You could loop through the file name checking characters until you hit a period. Then look at the next three characters. Most filenames only have the one period.

:blush: That's great but my problem is I don't know how to access the individual characters to loop through them. : embarrassed:

Something like this.

Uncompiled and Untested.

char extension[3];

for (int i=0;;i++)
{
  if (entryName[i] == '.')
  {
    for (int j=0;j<3;j++)
    {
      extension[j] = entryname[i + j];
    }

    break;

  }
  
  else
  {
    continue;
  }
  
}

would put the three characters you want into the array extension. You can use them however you want though. Once you know their index, you can just use the pointer like an array name.

This code gives you a char pointer to a string containing the file name:

  while(1){
    entry = root.openNextFile();
    entryName = entry.name();

If you want to know whether the filename ends in ".csv" there are various ways to do it. The simplest way is:

// uncompiled
int len = strlen(entryName);
if(len > 4)
{
    // could plausibly be x.csv
    if(strcmp(&entryName[len-4], ".csv") == 0)
    {
        // name ends in ".csv"
    }
    else
    {
        // name does not end in ".csv"
    }
}
else
{
    // too short to be a .csv file
}

PeterH:
This code gives you a char pointer to a string containing the file name:

  while(1){

entry = root.openNextFile();
   entryName = entry.name();

Oh. Obviously I have no working knowledge of pointers. I didn't realize I could simply do entryName[i] to access the i'th element as shown in Delta_G's code. Conceptually, I understood the pointer to be a just a "dumb" label for the string (I guess I thought I'd need a special function to read the string from the pointer).

Thanks PeterH for that snippet. I didn't realize those functions existed in the Arduino environment. I guess I need to do some more reading/digging.