I'm writing the following functions intending to return a copy of a stringstream
object. func_A
serves as a C wrap for some C++ functions. So the return value has to be char* or const char* for func_B()
and main
to use.
When func_A
is called I do see the printed out string, but I can not return the result_string
. After some googling I found out that you can not copy the result of stringstream
at all. I changed the return as result_string.str().c_str()
hoping that is can return a string. But that does not seem to work either. Any suggestions to how I can return a copy of the result_string
? For example, is there a way I can read the stringsteam
content and record it in a buffer?
const char* func_A(char* A, char* B, char* C){
// processing the string
// ...
std::stringstream result_string;
result_string << paramA << ":"
<< paramB << ":"
<< paramC;
// return the result string
printf("%s\n", result_string.str().c_str());
return result_string.str().c_str();
}
const char* func_B(){
...
...
return func_A(A, B, C);
}
int main(){
...
...
const char* buff = func_B();
printf("This is the content: %s", buff);
free(buff);
}