Lion King
Lion King

Reputation: 33813

How to put stringstream contents into char instead string type?

Every one know stringstream.str() need a string variable type to store the content of stringstream.str() into it .

I want to store the content of stringstream.str() into char variable or char array or pointer.

Is it possible to do that?

Please, write a simple example with your answer.

Upvotes: 9

Views: 32869

Answers (4)

zokia
zokia

Reputation: 63

I figured it out. Using namespace std and replacing tstingstreamwith stringstream. Next step is: stringstream strstream; strstream.imbue(std::locale("C")); string str = strstream.str(); const char *sql= str .c_str(); Now you can execute sql statement.

sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);

Maybe it helps to somebody.

Upvotes: 0

Vlad
Vlad

Reputation: 35584

Why not just

std::string s = stringstream.str();
const char* p = s.c_str();

?

Edit: Note that you cannot freely give the p outside your function: its lifetime is bound to the lifetime of s, so you may want to copy it.

Edit 2: as @David suggests, copy above means copying of the content, not the pointer itself. There are several ways for that. You can either do it manually (legacy way "inherited" from C) -- this is done with the functions like std::strcpy. This way is quite complicated, since it involves manual resources management, which is usually discouraged, since it leads to a more complicated and error-prone code. Or you can use the smart pointers or containers: it can be either std::vector<char> or std::unique_ptr/std::shared_ptr.

I personally would go for the second way. See the discussion to this and @Oli's answer, it can be useful.

Upvotes: 12

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153792

If you want to get the data into a char buffer, why not put it there immediately anyway? Here is a stream class which takes an array, determines its size, fills it with null characters (primarily to make sure the resulting string is null terminated), and then sets up an std::ostream to write to this buffer directly.

#include <iostream>
#include <algorithm>

struct membuf: public std::streambuf {
    template <size_t Size> membuf(char (&array)[Size]) {
        this->setp(array, array + Size - 1);
        std::fill_n(array, Size, 0);
    }
};

struct omemstream: virtual membuf, std::ostream {
    template <size_t Size> omemstream(char (&array)[Size]):
        membuf(array),
        std::ostream(this)
    {
    }
};

int main() {
    char   array[20];
    omemstream out(array);

    out << "hello, world";
    std::cout << "the buffer contains '" << array << "'\n";
}

Obviously, this stream buffer and stream would probably live in a suitable namespace and would be implemented in some header (there isn't much point in putting anything of it into a C++ file because all the function are templates needing to instantiated). You could also use the [deprecated] class std::ostrstream to do something similar but it is so easy to create a custom stream that it may not worth bothering.

Upvotes: 9

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272457

You can do this if you want an actual copy of the string (vital if the stringstream object is going to go out of scope at some point):

const char *p = new char[ss.str().size()+1];
strcpy(p, ss.str().c_str());

...

delete [] p;

As discussed in comments below, you should be wary of doing it like this (manual memory management is error-prone, and very non-idiomatic C++). Why do you want a raw char array?

Upvotes: 0

Related Questions