Pages: [1]   Go Down
Author Topic: Understanding Pointers and File Naming: SD Card Help?  (Read 530 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Code:
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!
« Last Edit: January 16, 2013, 07:41:24 pm by tms8c8 » Logged

Offline Offline
God Member
*****
Karma: 19
Posts: 790
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 


Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 




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

Offline Offline
God Member
*****
Karma: 19
Posts: 790
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like this.

Uncompiled and Untested.

Code:
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.  
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
 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:

Code:
// 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
}

Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
 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
Code:
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.
« Last Edit: January 16, 2013, 09:03:37 pm by tms8c8 » Logged

Pages: [1]   Go Up
Jump to: