need help about storing filenames of files in SD card. The filenames must be stored in an array of Strings for future use.
i've managed to print filenames in the serial monitor using the 'printDirectory' function, but i want to store them each in an array of String.
I have to display those filenames in 20x4 LCD, and add scrolling ability using my 4x4 keypad so i can scroll through filenames up and down. so i think it will be easier if i can store filenames in an array and use indexing to display them appropriately when scrolling up and down.
or do you have other ideas i can use aside from storing filenames in array?
An array would seem a reasonable method to hold the list of filenames but there will be a limit to how many you can hold due to memory limitations of the Arduino. Which board are you using ?
Are you sure that you need to store the names a Strings ? The advice from many here is to use C style strings (null terminated char arrays) instead but others favour usings Strings because of perceived ease of use. In your program you are already using both and convert between them, which does not seem sensible.
This is where the name is being returned, as a pointer to some storage location. Make a copy of the pointed to data, in some other memory location, and store the pointer to that memory location in an array.
UKHeliBob:
An array would seem a reasonable method to hold the list of filenames but there will be a limit to how many you can hold due to memory limitations of the Arduino. Which board are you using ?
Are you sure that you need to store the names a Strings ? The advice from many here is to use C style strings (null terminated char arrays) instead but others favour usings Strings because of perceived ease of use. In your program you are already using both and convert between them, which does not seem sensible.
You are storing a pointer in each location in the array. The SAME pointer. You need to save, instead, what the pointer points to. That could be done using strdup(). Keep in mind that strdup() allocates memory, so you need to free that at some time.
It would be best NOT to do that, though. Any time you need to display file names, get the names. Do not copy them.
thank you so much PaulS, its now working just as i want it.
but one more thing though,
can i dynamically resize the size of my char* array based on the elements i will store it into?
something like incrementing its size everytime i store something into it, rather than giving it a fixed size.
i want to use a for loop for displaying each item, something like
can i dynamically resize the size of my char* array based on the elements i will store it into?
No
The size of the array is fixed but you could either keep a record of the number of filenames in the array as you store them or put a dummy value into the next array position each time you save a filename and stop when you get to that dummy value when displaying the filenames.
can i dynamically resize the size of my char* array based on the elements i will store it into?
No
The size of the array is fixed but you could either keep a record of the number of filenames in the array as you store them or put a dummy value into the next array position each time you save a filename and stop when you get to that dummy value when displaying the filenames.
Even better would be to not use an array at all. Use a linked list. Each time you find a file, create a new node, store the file name in the node, and add the node to the linked list. A doubly linked list is not much more difficult, and can be traversed in both directions.
Don't forget to get the Arduino with a terabyte of memory, when you do this.
in the end, i used strdup() to store filenames in the char* array
//declared a global char* array
char* plantList[20]; //i used maximum of 20 elements to be stored
int listCounter=0; //variable to count how many filenames have been stored in the char* plantList
//inside the function of listing all files
char* temp = entry.name();
String toString = String(temp); //converted filename to String
toString = toString.substring(0, stri.length()-4); //made a substring to exempt the file extension(remove the *.txt)
toString.toCharArray(inString, toString.length()+1); //converted it back to char array for it to be stored in char* plantList
plantList[x]=strdup(inString);
Serial.print(plantList[x]);
listCounter++;
and now its flawLess.
Thank you very much for the help. XD