Jump to content
  • Advertisement

Archived

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

Dwiel

sprintf for strings

This topic is 5817 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

I was wondering how to use the sprintf type of function layout for a string... not a CString an std::string. there is an sprintf, but you need to have a buffer and so also must know a maximum size... Id really rather not need to know the max because im displaying a tree and because of its recursive nature it would be hell doing it this way.. is there a simple way i can some how do this:
  

//rather do it this way:


std::string str;
str = ***printf("x: %i\ny: %i\nz: %i\n", x, y, z);

//doing it this way:


std::string str;
str = "x: ";
str += x;
str += "\ny: ";
str += y;
str += "\nz: ";
str += z;
str += "\n";

  
the second way.. the current method is such a pain.. so i am looking for an alternative... thanx for the help ahead of time!!! dwiel ~ tazzel3d

Share this post


Link to post
Share on other sites
Advertisement
Use ostringstream.


  
ostringstream str;

str << "x: " << x << endl
<< "y: " << y << endl
<< "z: " << z << endl;
cout << str.str();



~~~~
Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
If you don''t want to use a stringstream, check out boost; IIRC, they had a formatting library in the works.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!