[SOLVED] using characters in char*

Hello,

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()
{
  
}

strcpy should do it.

Following on wildbill's suggestion, add:

char myFilename[14];

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().

Hello and welcome,

CottonEyeJoe: What I'm trying to do:

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

Thank you for you reply!

I am going to study and try this. I will let you know if it worked.

I see, I have definined entry twice by mistake. Going to change that.

Thanks!

guix: Hello and welcome,

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.

Thank you for your reply!

What I am trying to do is send a file with FTP.

link: http://playground.arduino.cc/Code/FTP

the filename there is 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.

So in your code you could simply use:

char *fileName = entry.name();

Tested it today and it works, thanks guys!

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.

To compare strings, you use function strcmp, which (again) will take pointers to strings, not strings themself.

https://www.cs.bu.edu/teaching/c/string/intro/ (especially part 3)

#guix: Some newbies might get the wrong idea from your statement:

To compare strings, you use function strcmp, which (again) will take pointers to strings, not strings themself.

It is, of course, OK to compare string literals, like:

void setup() 
{
  char name[] = "Jack";

  Serial.begin(115200);

  if (strcmp(name, "Jack") == 0) {     // Compare to a literal...
    Serial.println("Match");
  } else {
    Serial.println("No match");
  }
}

void loop() {
}

The idea was to make him realize that there is no need to copy the string (waste of memory) when he could simply use the original ;)

guix: To compare strings, you use function strcmp, which (again) will take pointers to strings, not strings themself.

https://www.cs.bu.edu/teaching/c/string/intro/ (especially part 3)

Thanks for the information.

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:

http://www.drdobbs.com/cpp/computer-programming-and-precise-termino/208808373?pgno=1

As you read this, keep in mind that a valid pointer variable can only hold one of two things:

  1. A valid memory address, meaning you can access that memory using indirection operator (*)
  2. 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, &)