473,387 Members | 1,517 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,387 software developers and data experts.

Converting sprintf to ostringstream

I'm converting some old C code to C++. Although I'm not concerned
about converting every C item to C++ I'm in a situation in which using
an ostringstream makes more sense than using sprintf. Here's my
problem: In sprintf, one could use the %g symbol to indicate that I
either wanted scientific notation or a simple floating point,
whichever is smaller. Is there a way to do that using an
ostringstream?

Basically, what is the ostringstream equivalent of:

sprintf(buffer, "%-20.12g",mynum);

Aug 23 '07 #1
2 2717
ja************@gmail.com wrote:
I'm converting some old C code to C++. Although I'm not concerned
about converting every C item to C++ I'm in a situation in which using
an ostringstream makes more sense than using sprintf. Here's my
problem: In sprintf, one could use the %g symbol to indicate that I
either wanted scientific notation or a simple floating point,
whichever is smaller. Is there a way to do that using an
ostringstream?

Basically, what is the ostringstream equivalent of:

sprintf(buffer, "%-20.12g",mynum);
I would think it does that by default, no? Just output 'mynum' and
it should do what you need. As to '20' and '12', look for the 'setw'
and 'setprecision' manipulators.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 23 '07 #2
Ah... I see. I was trying too hard. Yes it does do it like the %g
by default. Thanks!

Aug 23 '07 #3

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

Similar topics

4
by: Alex Vinokur | last post by:
Is it possible to use vector<ostringstream> ? Here is what I have got. =========================================== Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927...
4
by: x | last post by:
converting 1944 to '1','9','4','4' how can I convert a number such as 1944 to a character array? thanks!
2
by: bballmitch | last post by:
For some reason the following code is not compiling. void Find_score(void) { glColor3f(1.0,0.0,0.0); std::ostringstream ostr; //turn int score into a string ostr << score; ...
11
by: Frank Neuhaus | last post by:
Hey Iam occaisonally using something like char buffer; sprinf(buffer,"Hello test %d %s",someint,somestring); Is there any _convenient_ and buffer overflow secure equivalent for that using...
4
by: adamrobillard | last post by:
Hi, I went through most of the FAQ but did not find any direct references formatting outputed data. If I have a application in C like so: void main(void) { int nValue = 100; double...
11
by: TomServo | last post by:
I am writing code that needs to run on a variety of Unix systems. I am calling the statvfs and statfs system calls and I need to to convert some of the integers returned to character strings....
3
by: Susan Rice | last post by:
How can I rewrite this code to avoid the possibility of a buffer overflow? sprintf(errbuf, "%s\nError is: %u: %s\n", errmsg, dwErrCode, s ); Here: errmsg = a string dwErrCode = a number s =...
5
by: 2b|!2b==? | last post by:
I am not making much progress porting some old C code to C++ ... I am porting the lines below, which prints an ISO8601 date string: char buff ; memset(buff, '\0',16);...
44
by: A.Leopold | last post by:
hello, is there a c++ equivalent function to the c-function 'sprintf'? Thank you, leo
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.