sprintf() for std::string

Started by
6 comments, last by FrigidHelix 21 years, 10 months ago
What is a good way to output a variable's value to std::string? I keep having to litter my code with temporary character buffers to then use sprintf(), and then build a std::string out of this buffer, in order to represent a variable's value in a string. Surely, there must be a better way of doing this. Why else would the standards committee omit a formatting member function. (Even Microsoft's CString class has a very convienient Format member function that acts like sprintf() for the string). Does std::string just suck? or am i not seeing the elegant approach to this? Edit: clarified that by string i mean std::string [edited by - FrigidHelix on June 3, 2002 3:15:01 AM]
Advertisement
You can do the following:


  string szTempString;szTempString = "a test!";printf("This is %s", szTempString.c_str());  


c_str will convert the string to a constant character reference. Keep in mind though that it is constant, you can''t use it in a itoa function or such.
------Shop for the Lowest Price!Then, Check a Reseller's Rating Before You Purchase!
Maybe you mean mean sth. like this:

  #include <string>#include <strstream>#include <iostream>using namespace std;int main(int argc, char *argv[]){    strstream stream;    int   x = 10;    float y = 0.01f;    char  z = 'z';    stream << "int x = " << x << endl;    stream << "float y = " << y << endl;    stream << "char z = " << z << endl;    cout << stream.str() << endl;    //cout << stream.str().c_str() << endl; // use this with msvc++    cin.get();    return 0;}  



[edited by - baumep on June 3, 2002 3:30:46 AM]
baumep
baumap - UGH! strstream is deprecated.

This is how it should be:

  #include <string>#include <sstream>#include <iostream>using namespace std;int main(){    ostringstream stream;    int   x = 10;    float y = 0.01f;    char  z = ''z'';    stream << "int x = " << x << endl;    stream << "float y = " << y << endl;    stream << "char z = " << z << endl;    cout << stream.str() << endl;}  


--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Just a relatively off topic note on aNonamuss'' post: about szTempString, if you''re trying to follow Hungarian notation ''n all that, mebbe you would like to look at changing that to strTempString, as szTempString implies that this is a zero-terminated string, instead of an instantiation of a string class.
Beautiful. Its the same type of syntax as iostreams. thx.
I found out that if you use the str() member function, the automatically created buffer is "frozen", such that no more characters can be added to the stream, and the user is responsible for deallocating it.

Here are 2 possible solutions:
1) delete the buffer yourself to prevent memory leak:
delete[] stream.str();
2) remove the "freeze" so u can keep adding characters, and the buffer will be deallocated when the stringstream object goes out of scope:
stream.rdbuf()->freeze(0);
quote:Original post by FrigidHelix
I found out that if you use the str() member function, the automatically created buffer is "frozen", such that no more characters can be added to the stream, and the user is responsible for deallocating it.

Here are 2 possible solutions:
1) delete the buffer yourself to prevent memory leak:
delete[] stream.str();
2) remove the "freeze" so u can keep adding characters, and the buffer will be deallocated when the stringstream object goes out of scope:
stream.rdbuf()->freeze(0);


Don''t use the char*-based strstream, from <strstream>, they are deprecated ! Use the std::string-based std::stringstream from <sstream>.

That way, you are guaranteed not to have memory leaks, and neither ''freeze'' problems.

Arild Fines warned you, you didn''t listen, now you''re paying the price.


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement