How to put stringstream contents into char instead string type?

后端 未结 4 660
滥情空心
滥情空心 2021-01-17 21:10

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

I want to store the cont

相关标签:
4条回答
  • 2021-01-17 21:15

    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.

    0 讨论(0)
  • 2021-01-17 21:19

    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.

    0 讨论(0)
  • 2021-01-17 21:35

    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?

    0 讨论(0)
  • 2021-01-17 21:37

    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.

    0 讨论(0)
提交回复
热议问题