Search code examples
c++stringc-str

How to cast a std::stringbuf into an array of char?


What I'm trying to do here is to cast a stringbuf object into an array of char.

I do this to send the array of char to a C interface which doesn't understand the type std::stringbuf.

Here's a part of my code to illustrate the problem :

std::stringbuf buffer;
char * data;

//here i fill my buffer with an object
buffer >> Myobject;
//here is the function I want to create but I don't know if it's possible
data = convertToCharArray(buffer);
//here I send my buffer of char to my C interface
sendToCInterface(data);

Solution

  • As Kiroxas suggests in the first comment, try to avoid intermediate variables:

    sendToCInterface(buffer.str().c_str());
    

    ...the less variables the less confusion ;-)