Hello
This stupid problem is taking me hours to solve....
I have a name for a data file and I can retrieve that name as such:
char SDname[20];
Serial.print("SDname: ");
for (int i=1; i<9; i++){
Serial.print(SDname[i]);
}
I want to compare this 8 character name to the files already on the sd card using:
if (SD.exists(SDname)) {
Serial.println("That file name already exists");
}
How do I convert the data in my char to something I can use to search with?
Thanks
You really should post your entire sketch in order to get help. I am confused by this:
char SDname[20];
Serial.print("SDname: ");
for (int i=1; i<9; i++){
Serial.print(SDname[i]);
}
This doesn't print the first character of the file name. Also, where are you initializing SDname? Without those details it will be hard to help.
By the way, the methods in the SD class will take String or 0-terminated character strings as input. Your comparison code should work properly assuming your SDname string is 0-terminated. Since the use of String is discouraged I would use 0-terminated character strings.
Thanks. Sussed it out just after I posted (as usual)
SDtemp[0] = MacroName[1];
SDtemp[1] = MacroName[2];
SDtemp[2] = MacroName[3];
SDtemp[3] = MacroName[4];
SDtemp[4] = MacroName[5];
SDtemp[5] = MacroName[6];
SDtemp[6] = MacroName[7];
SDtemp[7] = MacroName[8];
Then compare SDtemp with the SD card contents
Your indexing apparently begins at 1. What is in SDname[0]? When you pass SDname to the library functions, whatever character is in SDname[0] will be appended to the beginning of the name.
Yes, I noticed that. I fixed the 'for loop' to read from 0.
It works, but it's ignoring spaces in the name.
So "Test1" is found if it exists, but "Test 1" isn't found (both those names are on the SD card).
Hmmm
I tried (clearly incorrectly....) to detect the spaces (MacroName==32) and manually adding the space with strcat. This routine does find files fine without spaces.
if (MacroName[0]==32){strcat(SDtemp, " ");} else {SDtemp[0] = MacroName[0];}
if (MacroName[1]==32){strcat(SDtemp, " ");} else {SDtemp[1] = MacroName[1];}
if (MacroName[2]==32){strcat(SDtemp, " ");} else {SDtemp[2] = MacroName[2];}
if (MacroName[3]==32){strcat(SDtemp, " ");} else {SDtemp[3] = MacroName[3];}
if (MacroName[4]==32){strcat(SDtemp, " ");} else {SDtemp[4] = MacroName[4];}
if (MacroName[5]==32){strcat(SDtemp, " ");} else {SDtemp[5] = MacroName[5];}
if (MacroName[6]==32){strcat(SDtemp, " ");} else {SDtemp[6] = MacroName[6];}
if (MacroName[7]==32){strcat(SDtemp, " ");} else {SDtemp[7] = MacroName[7];}
strcat(SDtemp, ".txt"); // Add the file extension
if (SD.exists(SDtemp)) {
Serial.println("That file name already exists");
}
Keep in mind strcat cannot work properly unless the string you are cat'ing to is 0-terminated!
Does the SD library even support spaces in the file name? IIRC it's pretty crude in how it handles the file system.
Err.... dunno!
This SD card thing has taken hours. Don't entirely understand the strcat affair. Further Googling required.
If you list the contents/files of the sd card, the spaces are there in the names. So it must recognise them?
I will keep hacking away at it
ToddL1962:
Keep in mind strcat cannot work properly unless the string you are cat'ing to is 0-terminated!
Not sure even what that means.
I am putting the data into: char SDtemp[9]; That I assume is incorrect.
phoneystark2020:
I am putting the data into: char SDtemp[9];
What does that mean? We let you get away without posting all your code. See what happens?
The code is huge. I will have to convert it to a text file then or something.
Indeed the SD library does not handle spaces either very well or at all.
I can read the filenames off the SD fine, so I think the way forward it to do that in a loop and compare those retrieved names with my new name, rather than use the SD.exists function.
I'll convert the code later after a bit of a tidy.
Fixed the problem. I put a line in the SD file name creation that converted spaces to underscores.
Now there is never a space in the name to cause an issue
phoneystark2020:
Err.... dunno!
This SD card thing has taken hours. Don't entirely understand the strcat affair. Further Googling required.
If you list the contents/files of the sd card, the spaces are there in the names. So it must recognise them?
I will keep hacking away at it
Not sure even what that means.
I am putting the data into: char SDtemp[9]; That I assume is incorrect.
Remember in reply #3 when I said add this line?
SDtemp[8] = 0;
That is a 0-termination. Otherwise, there is no way for the code to know where the string ends.