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" ?
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).
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.