473,413 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

std::string sprintf

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
Oct 7 '05 #1
11 118490

"Frank Neuhaus" <fn******@uni-koblenz.de> wrote in message
news:di**********@cache.uni-koblenz.de...
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::ostringstream'

-Mike
Oct 7 '05 #2
Frank Neuhaus wrote:
Hey
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::ostringstream os;
os << "Hello test " << someint << ' ' << somestring;
std::string buffer(os.str());

// here 'buffer' has the string

V
Oct 7 '05 #3
Frank Neuhaus wrote:
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.

--
Matthias Kaeppler | Tel: +49 631 3405805
Gerhart-Hauptmann-Str. 16a | Mob: +49 176 20108693
D-67663 Kaiserslautern | E-Mail: matthias at finitestate dot org
Oct 7 '05 #4
Frank Neuhaus wrote:
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


http://www.parashift.com/c++-faq-lit...es.html#faq-39.
1
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Oct 7 '05 #5
Frank Neuhaus wrote:
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

Default User <de***********@yahoo.com> wrote: http://www.parashift.com/c++-faq-lit...es.html#faq-39.
1


Link should be all on one line:

http://www.parashift.com/c++-faq-lit....html#faq-39.1

--
Marcus Kwok
Oct 7 '05 #6
Thanks for all suggestions
ostringstream looks like the most reasonable method for my case ;)
Gonna look into that

"Frank Neuhaus" <fn******@uni-koblenz.de> schrieb im Newsbeitrag
news:di**********@cache.uni-koblenz.de...
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

Oct 7 '05 #7
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::ostringstream o;
if (!(o << x))
throw BadConversion("stringify(double)");
return o.str();
}

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

Oct 7 '05 #8
we*****@yahoo.com wrote:
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::ostringstream o;
if (!(o << x))
throw BadConversion("stringify(double)");
return o.str();
}

how expensive to create std::ostringstream 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
Oct 7 '05 #9
Marcus Kwok wrote:

Link should be all on one line:

One of the few bad quirks of XanaNews is wrapping long URLs.

Brian
Oct 7 '05 #10
John Harrison wrote:
we*****@yahoo.com wrote:
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::ostringstream o;
if (!(o << x))
throw BadConversion("stringify(double)");
return o.str();
}

how expensive to create std::ostringstream 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.

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
Oct 7 '05 #11
we*****@yahoo.com wrote:
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::ostringstream o;
if (!(o << x))
throw BadConversion("stringify(double)");
return o.str();
}

how expensive to create std::ostringstream 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.
Oct 7 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
8
by: Woodster | last post by:
I am starting to use templates and have managed to figure out how to use std::string, std::map and make_pair successfully so far (Yeah I know - not much of a big step but I'm getting there) ...
7
by: Sims | last post by:
Hi, if i have a code const char * GetValue() { std::string szVectorValue = ...// get a std::string from the vector return szVectorValue.c_str(); }
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
13
by: Glen Able | last post by:
Obviously the following doesn't work: int i = 5; std::string myString = "Number is " + i + " thankyou please"; So can anyone give me some idea what's the nicest way to this sort...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
12
by: sas | last post by:
hi, i need that because the path functions for windows, like PathAppend and PathRemoveFileExt accept a writable zero terminated char*, but i didn't find that for std::string, with CString, i...
4
by: Ramesh | last post by:
Hi, I need to convert unsigned long data to a std::string, I googled a bit to see if the string class supports any methods to achieve this - but didnt find any, I think of using sprintf and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.