Check for a part of a filename / searching files with certain name in a path

Hi

Assuming I read a directory of a SD card I get a list of filenames in return, which are of type char.
Among these are files named what12.txt, what234.txt, what2.txt and so on.

Now how can I do a simple
"look for files with a certain name" THEN "pick the one with the highest ID?

While this is such a simple task in almost ANY language it makes me fail totally at c++.

For obvious reasons (simulator) a testcode without SD reading:

#include <SoftwareSerial.h>  
void setup() {
  Serial.begin(115200);
}
void loop() {
char buffer[]="what12.txt";
char returnChar[4]; 
char * ptr = strchr(buffer, 'w'); 
strncpy(returnChar, ptr, 4);
 Serial.println(returnChar);

 // of course NOT working as I assume a string here :-(
 if (returnChar=="what") {
    Serial.println(" hit -what-");
 }
}

I know I can convert the ID int with atoi() for the math for the highest id but
how can I do the comparison for "what" ?

You can't use == to compare char arrays. Use strcmp()

1 Like

You use the strcmp() function, but only after you have correctly terminated the target string with a '\0'

Don't forget that a string containing "what" needs an array of 5 elements so that the '\0' can be used as its terminator

1 Like

strstr might be of help.

1 Like

see strstr() will find the "what" in what12.txt

1 Like

Bob is correct (see this page). So you will need to increase the size of your array, to hold that null-terminator:

char returnChar[5];
1 Like

Yes, handling and manipulating text is not as easy in C/C++ as in many other languages. But those languages won't run on most Arduino, because they require far more resources (memory & processing speed) than most Arduino have.

Some more modern and advanced Arduino boards, with more resources, can run Micro-Python or Circuit-Python or similar. You would find that, if you wrote the nearest equivalent code in C/C++ and in Python (each code written by a coder competent in that language) the C/C++ code would probably run 10+ times faster and use 10 times fewer resources, compared to Python. That's why C/C++ was chosen as the original Arduino language.

You can make use of the String library in C/C++, which makes this kind of thing as easy as it is in most other languages. But that library requires more resources and can be a problem on basic Arduino boards (unless you are careful and you know what pitfalls to avoid).

1 Like

thanks guys, this works:

 if (strstr(returnChar,'what')) {
    Serial.println(" hit -what-");
 }

I guess strcmp too but looks more complicated to me. Advantage/disadvantage?

Both functions are not in the arduino reference?

strcmp() is a simpler function than strstr().

But the advantage of strstr() is that you would not have to use strncpy() first to take the first 4 characters out of the file name. You can use strstr() directly on the filename, which you don't seem to be doing in post #8.

1 Like

Oh, are you sure? That looks wrong to me. Wrong type of quote marks. ' is for single characters (char). " is for strings/char arrays.

Well, it did in a browser simulator , might give trouble on the real device then.

EDIT:yes, you are right, ' ' gives an error in IDE

1 Like

You're making life complicated; you have found "what" but you haven't gotten the numbers following that.

Simple demo to print everything following "what" (not tested).

char *ptr = strstr(someName, "what");
if (ptr != nullptr)
{
  ptr+=strlen("what");
  Serial.println(ptr);
}
1 Like

yes so true ! plus:
Serial.println(atoi(ptr));
and I got the number

maybe c++ isn't so bad at all at the end :wink:
thanks!

Does your code so what you described, ie

I'm surprised that that works, but it is not correct. Use double quotes for C-strings, as follows.

if (strstr(returnChar,"what")) {

as in the follow up comments, it only works in the simulator, check for yourself:

Then the simulator is wrong, as well as the code.

indeed

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.