Stream Thy Strings

http://www.artima.com/cppsource/streamstrings.html

Makes thy classes OutputStreamable using std::ostringstream and std::istringstream.

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
    ostringstream os;

    os <"hi" << endl;
    os <123 << endl;

    string str = os.str();

    istringstream is;
    is.str(str);

    string msg;
    int i;

    is >> msg;
    is >> i;

    cout << msg <"," << i << endl;

    return EXIT_SUCCESS;
}

stringstream class is a better solution than sprintf/vprintf/snprintf/vnprintf, each of which is quite easy to be exploited by malicious users.

Similar Posts:

Post a Comment

Your email is never published nor shared.