Archived

This topic is now archived and is closed to further replies.

sprintf() for std::string

This topic is 5675 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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;
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites