Go Down

Topic: Understanding Pointers and File Naming: SD Card Help? (Read 551 times) previous topic - next topic

tms8c8

Jan 17, 2013, 01:38 am Last Edit: Jan 17, 2013, 01:41 am by tms8c8 Reason: 1
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: [Select]

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!

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. 



tms8c8


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:

Delta_G

Something like this.

Uncompiled and Untested.

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

PeterH

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

Code: [Select]

 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: [Select]

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


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

tms8c8

#5
Jan 17, 2013, 03:01 am Last Edit: Jan 17, 2013, 03:03 am by tms8c8 Reason: 1

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

Code: [Select]

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

Go Up