sprintf equilvalent in C++ ?

T

tvn007

Hi,
Is there a sprintf equilvalent in C++ ?

I have to do the following segment of my code:
////////////////////////////////////////////////////////////////
string test("twice");
char assignment[40];
sprintf (assignment, "do run \"%s\";",test.c_str());
///////////////////////////////////////////////////////////////////////
 
M

Marcin Kalicinski

You can use stringstream:

std::stringstream stream;
stream << "do run \"" << test << "\";";
std::string assignment(stream.str());

or alternatively use boost::format (see www.boost.org)

cheers,
Marcin
 
C

Colander

std::string has an append function;

std::string assignment("do run \"");
assignment.append(test);
assignment += "\""; // += aliasses append...
 
T

tvn007

Marcin , your solution seems to work since not only string, I have to
add integer and double to sprintf.

Thanks
 
D

Dirk Clasen

int someInt = 5;
double someDouble = 10.12;
std::stringstream stream;
stream << "do run \"" << test << "\";" << someInt << " " << someDouble;
std::string assignment(stream.str());
 
J

John Harrison

tvn007 said:
Marcin , your solution seems to work since not only string, I have to
add integer and double to sprintf.

Thanks

Boost::format works with integer and double, and is just like sprintf.
Check it out.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top