473,804 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2736
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
6210
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 (prerelease) ===========================================
4
412
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
2371
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; //score is defined some where else std::string LL = ostr.str();
11
118535
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 std::string? Thanks
4
2759
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 dPi = 3.14159265; char sName = {"Morpheus"};
11
3375
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. Normally I would do this using sprintf as so: sprintf(&buffer, "%lu", integer); The problem is that I will not know if the integer values I am converting are long or long long. I also do not know if the compilers being used will support long long...
3
6465
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 = a string I do have value 'errbuflen' = length of buffer 'errbuf'.
5
4459
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); sprintf(buff,"%02d:%02d:%02d", hours, minutes, seconds) ; to :
44
7849
by: A.Leopold | last post by:
hello, is there a c++ equivalent function to the c-function 'sprintf'? Thank you, leo
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10561
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.