# sprintf() equivalent for std::string?

## Recommended Posts

synth_cat    306
Hello all! I am currently making a settings configuration program and I want a quick way to be able to print a single std::string representing a display mode. To do this, it would be very helpful to have something similar to sprintf() to quickly add non-string enitities like integers to my display mode string. I tried the following, and it didn't work (I didn't expect it to.) But it gives you the idea of what I'm trying to do.
std::string teststring="The number is:  ";
int test_int=1000;
sprintf(teststring.c_str(),"%d",test_int);


Thanks for any help! -synth_cat

##### Share on other sites
Jan-Lieuwe    206
Something like:

int test_int=1000;
std::sstream teststring << "The number is: " << test_int;

// to get the std::string type: teststring.str()

That's why I prefer AnsiString over std::string :)

##### Share on other sites
Lajnold    226
Or get boost::format.

##### Share on other sites
synth_cat    306

I think I've found out the way you're supposed to do this, using an ostringstream object. This is outlined

here
.

However, I don't know how to cast an std::ostringstream to a const char* value so I can use TextOut() to print it. I could do this with an std::string with the c_Str() member function, but std::ostringstream seems to lack this method.

Can anyone help me out?

##### Share on other sites
cmptrgear    306
int anumber = 10;std::stringstream stream;stream << "The number is: " << anumber;const char * cptr = stream.str().c_str();

##### Share on other sites
hh10k    589
Quote:
 Original post by cmptrgearconst char * cptr = stream.str().c_str()

This looks dangerous, since stream.str() is very likely to create a temporary std::string instance which will be immediately destructed again, taking the c_str with it. cptr will then point to deallocated memory. Try to consistantly pass around std::strings instead of bare char*s, or be very careful with your object lifetimes :)

##### Share on other sites
Aardvajk    13207
hh10k is correct.

It would be quite acceptable to do this:

ostringstream Os; Os << 23;TextOut(Hw,10,20,Os.str().c_str());

since you are only using the pointer inside the expression in which the temporary std::string is created. However, if you need to use the text data in subsequent statements, you need to do:

ostringstream Os; Os << 23;std::string s=Os.str();TextOut(Hw,10,20,s.c_str());SomeOtherFunctionTakingConstCharStar(s.c_str());

##### Share on other sites
synth_cat    306
Thanks a lot - I understand better now.

here
.
string str;ostringstream strstrm;strstrm << boolalpha << true << " is a bool value" << noboolalpha         << ", so is " << false; cout << strstrm.str() << endl;strstrm.str("");strstrm << "100 decimal = " << dec << 100 << ", octal = " << 100         << ", hex = " << hex << 100 << endl;cout << strstrm.str();strstrm.str("");float f = 199.9273f;strstrm << "A float: " << f << ", scientific notation " << f         << " " << scientific << f << " fixed notation " << fixed         << f << endl;cout << strstrm.str();strstrm.str("");strstrm << "Formatted to two decimal places " << setprecision(2) << f << ", formatted to %2.5f " << setprecision(5) << leftprec(f, 2) << endl; cout << strstrm.str();

Basically, why is there an std::string in this snippet that is never used? Does std::ostreamstring automatically output to the last std::string created?

Also, is it possible to use the << operator to take an ostreamstring object and write a string to itself like so?
std::ostreamstring stream;std::string text = "String Text";stream << "The string says " << text;

##### Share on other sites
jyk    2094
Quote:
 Original post by synth_catBut I still have a question about this ostringstream object. This comes from the site where I first found out about ostringstream, here.*** Source Snippet Removed ***Basically, why is there an std::string in this snippet that is never used?
The variable str in the example appears to be extraneous.
Quote:
 Also, is it possible to use the << operator to take an ostreamstring object and write a string to itself like so?*** Source Snippet Removed ***
You're not 'writing the string to itself' (the string and the stringstream are different objects), but yes, you can insert a string into a stringstream just like with any other output stream (ostream, ofstream, etc.).