std::string sprintf

F

Frank Neuhaus

Hey
Iam occaisonally using something like
char buffer[1024];
sprinf(buffer,"Hello test %d %s",someint,somestring);

Is there any _convenient_ and buffer overflow secure equivalent for that
using std::string?
Thanks
 
M

Mike Wahler

Frank Neuhaus said:
Hey
Iam occaisonally using something like
char buffer[1024];
sprinf(buffer,"Hello test %d %s",someint,somestring);

Is there any _convenient_ and buffer overflow secure equivalent for that
using std::string?

Look up 'std::eek:stringstream'

-Mike
 
V

Victor Bazarov

Frank said:

Hey yourself
Iam occaisonally using something like
char buffer[1024];
sprinf(buffer,"Hello test %d %s",someint,somestring);

Is there any _convenient_ and buffer overflow secure equivalent for that
using std::string?

There is a snprintf, IIRC...

Generally speaking, the only reasonable way I know is

std::eek:stringstream os;
os << "Hello test " << someint << ' ' << somestring;
std::string buffer(os.str());

// here 'buffer' has the string

V
 
M

Matthias Kaeppler

Frank said:
Iam occaisonally using something like
char buffer[1024];
sprinf(buffer,"Hello test %d %s",someint,somestring);

Is there any _convenient_ and buffer overflow secure equivalent for that
using std::string?

Boost.Format has printf-style formatting by overloading operator%. It
still doesn't feel very printf-ish to me though. What you do, basically,
is create a formatter object which has an internal stream buffer holding
the formatted sequence. You can then take its string value and assign it
to a std::string.

Example:

std::string s;
s = str( format(" %d %d ") % 11 % 22 );

See http://www.boost.org/libs/format/index.html for more examples.
 
M

Marcus Kwok

F

Frank Neuhaus

Thanks for all suggestions
ostringstream looks like the most reasonable method for my case ;)
Gonna look into that
 
W

wenmang

I am read the example in your link, and I have a question about
efficiency of following code:

inline std::string stringify(double x)
{
std::eek:stringstream o;
if (!(o << x))
throw BadConversion("stringify(double)");
return o.str();
}

how expensive to create std::eek:stringstream o again and again by calling
stringify() compared to sprintf() C function call?
 
J

John Harrison

I am read the example in your link, and I have a question about
efficiency of following code:

inline std::string stringify(double x)
{
std::eek:stringstream o;
if (!(o << x))
throw BadConversion("stringify(double)");
return o.str();
}

how expensive to create std::eek:stringstream o again and again by calling
stringify() compared to sprintf() C function call?

sprintf is much cheaper than ostringstream *except* when you get a
buffer overflow, when it is much, much, much more expensive.

john
 
D

Default User

John said:
sprintf is much cheaper than ostringstream except when you get a
buffer overflow, when it is much, much, much more expensive.


And the original request specifically asked:

"Is there any convenient and buffer overflow secure equivalent for that
using std::string?"

He already had an sprintf() solution.



Brian
 
J

Jacques Labuschagne

I am read the example in your link, and I have a question about
efficiency of following code:

inline std::string stringify(double x)
{
std::eek:stringstream o;
if (!(o << x))
throw BadConversion("stringify(double)");
return o.str();
}

how expensive to create std::eek:stringstream o again and again by calling
stringify() compared to sprintf() C function call?

On my system the actual conversion is roughly as fast as through
sprintf, but the cost of creating and destroying a stringstream for each
conversion triples the time taken.
I've always used stringstream for the general form of my "stringify"
equivalent and then specialised the numeric versions to use snprintf.

Jacques.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top