getting a String from a library (.cpp) to a sketch?

Hi,
I've been wandering around in the land of pointers for a while now, but am still stuck with the question:

Is it possible to get a string that is created in the library (.cpp) to the sketch, and how would one go about this?

Many thanks!

Is it possible to get a string that is created in the library (.cpp) to the sketch, and how would one go about this?

Yes, it is.

Is that a string with a lower case s (an array of characters) or a String object (with an upper case s)?

What code do you have that is creating the string or String?

Hi Paul, thanks for your reply.
It's an array of characters ( byte buffer[512]; ), picked from an SD card by SDuFAT.cpp, that in the original library is sent to to the serial port by means of:

(...excerpt from SDuFAT.cpp)
for(int i = 0; i < length; ++i)
{
  if (buffer[i] == EOF) {
  Serial.println();
  return 0;
}
  Serial.print(buffer[i], BYTE);
}

but I would like to get the string to the main sketch for it to be reworked. I am however lost in the forrest of pointers and globals.
I could start working in the library, but don't think that's the way to go and would like to understand how to do this.

Many thanks

That library seems pretty limited. It doesn't have a method to read data from a file on the SD card.

Of course, all the lower level stuff that it calls in SDuFAT::cat() can be called from your sketch, too. Then, in the for loop, do whatever you need to do with the data, instead of printing it to the serial port.

Hi,
The capabilities of the library itself suffice for now, I'm just wondering regardless of this library how to get a string that is created in the library, to the sketch.
Perhaps a basic thing, but I just can't find out how.

Perhaps a basic thing, but I just can't find out how.

It really isn't a difficult thing to do, but it is something that the library should provide, in the form of a read() method that allows the user to define how much data to read from a file.

If you want to create this capability in the sketch, just copy the code from the cat method, and make a few changes.

Perhaps I should rephrase,
How would you, in any library not necessarily this one, have a string made in the library, and then be able to read or use it in the sketch?

i.e. in the library (example.cpp file) there is:

byte justastring = "hello";

how can I in the sketch then use that string?

I suppose it has to do with it being global, and it's declaration in the sketch etc.?

I suppose it has to do with it being global, and it's declaration in the sketch etc.?

If the variable is global, it doesn't matter whether it was valued in the sketch or in a library function. The data will be accessible from the sketch.

If the variable is local to the sketch, then the value of the variable may be able to be returned by a library function. digitalRead is a library function. It is able to return a value.

Returning data that is defined on the heap in a library function, as in the case of

char *hi = "Hello world";

is NOT possible.

byte justastring = "hello"; is not syntactically valid. You can not store a string in a byte.

Many thanks, I've managed for now by moving the function to the sketch as you advised, have to say am still riddled as to how to be able to use a string generated in the library in the sketch.
Perhaps a bit too ++ for me..

have to say am still riddled as to how to be able to use a string generated in the library in the sketch.

You would need to point out which string "generated in the library" you wanted to use in the sketch.

If you are referring to the array that the cat function uses to hold data read from the file (buffer), you need to be aware that the array only holds a (potentially) small amount of the file (512 bytes) at any one time. When the cat() function ends, only the last few bytes of the file (up to 512) are in the array.

Since the array is global, you just need to tell the sketch that it exists:

extern byte buffer[];

Ah perhaps the 'extern' is what I was after, will give it a try, thanks