I am working on a project, and I am stuck for a few days now.
What I'm trying to do:
Get a filename from the SD card and use it somewhere else in the program. I need the filename as a string
exp: char fileName[13];
The problem:
I use: entry.name() to get the name, and when I print it to the serial monitor I get the filename but it's not a string. I have done some research and I think it returns the filename as a pointer(?) I have tried to learn something about pointers and I know a bit about pointers now, but I dont know how to use them exactly.
The question:
How can I get the characters into a string?
code:
#include <SPI.h>
#include <SD.h>
File entry;
void setup()
{
Serial.begin(9600);
pinMode(4, OUTPUT);
SD.begin(4);
File entry;
entry = SD.open("/").openNextFile();
Serial.println(entry.name());//this works fine, the file name is printed in the serial monitor
}
void loop()
{
}
immediately after the opening brace of setup(), then add:
[code]
strcpy(myFilename, entry.name());
[/code]
just after the open() function call. BTW, you have entry defined at two different places in your program...not a good idea. If you don't want to pass that variable around to other functions throughout your program, leave the one with global scope (i.e., the definition at the top of the file) in effect and erase the one defined within setup(). If you want to "hide" (encapsulate) it, get rid of the global definition and keep the local one defined within setup().
Get a filename from the SD card and use it somewhere else in the program.
Can you explain exactly how will you use it? What are you trying to do with the file name? Because in many cases you don't need to convert a char pointer to a string.
Or did you mean, a String (object of the String class) instead of a string (null-terminated char array) ?
Can you explain exactly how will you use it? What are you trying to do with the file name? Because in many cases you don't need to convert a char pointer to a string.
Yes it's a string in that example, but that doesn't mean that you have to use a string in your program. In fact, those functions (SD.open etc) doesn't take the string as parameter, but a pointer to it.
guix:
Yes it's a string in that example, but that doesn't mean that you have to use a string in your program. In fact, those functions (SD.open etc) doesn't take the string as parameter, but a pointer to it.
I will see if I can get that working too, just for my information. But I have to compare the name with other strings, so I think putting the name in a string is the best way to do that.
And yes, it works this way but I don't know exactly why. I also want to know why it works and completely understand pointers. Therefore I am very happy with in information in the link.
Pointers are where a huge chunk of C's power comes from. They are not difficult once you understand what they are. See if this helps:
As you read this, keep in mind that a valid pointer variable can only hold one of two things:
A valid memory address, meaning you can access that memory using indirection operator (*)
null. The pointer contains garbage.
Unfortunately, an uninitialized pointer may not be null, but it's unlikely that it points to anything useful until you explicitly set it to the memory address of a variable (often using the address-of operator, &)