473,387 Members | 1,812 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.

sprintf equivalent

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[256] = {"Morpheus"};

printf("Name: %10s, Value: %4d, Pi: %0.2f", sName, nValue, dPi);
getchar();
}

How would I format the output using cout and string variables. Is it
valid to use sprintf to format your output then use cout to display it?
If so, how can you sprintf into a string?

I have read up on .precision etc but after several lines of code I
still do not end up with the results I am looking for. Is there a one
line solution like in C?

Thanks,

Nov 22 '05 #1
4 2738
ad***********@gmail.com wrote:
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[256] = {"Morpheus"};

printf("Name: %10s, Value: %4d, Pi: %0.2f", sName, nValue, dPi);
getchar();
}

How would I format the output using cout and string variables. Is it
Something like this

cout << "Name: " << setw(10) << sName << ", Value: " << setw(4) <<
nValue << fixed << setprecision(2) << dPi;

I might have got some of the details wrong. As you can see this stuff is
very tedious.
valid to use sprintf to format your output then use cout to display it?
Yes.
If so, how can you sprintf into a string?
That's not possible. The nearest equivalant is to use a stringstream.

ostringstream buf;
buf << "Name: " << setw(10) << sName << ", Value: " << setw(4) <<
nValue << fixed << setprecision(2) << dPi;
string str = buf.str();

I have read up on .precision etc but after several lines of code I
still do not end up with the results I am looking for. Is there a one
line solution like in C?
No, unless you are prepared to write a lot of code. Fortunately someone
has already done this for you

http://www.boost.org/libs/format/doc/format.html

john


Thanks,

Nov 22 '05 #2
I have read up on .precision etc but after several lines of code I
still do not end up with the results I am looking for. Is there a one
line solution like in C?


I guess you missed that

cout.precision(10);
cout << x;

can be written in one line like this

cout << setprecision(10) << x;

You need to #include <iomanip>

John
Nov 22 '05 #3
A bit messy compared to C but definitly workable. I will try that, and
look at the boost library.

Thanks,
Adam

Nov 22 '05 #4
ad***********@gmail.com wrote:
A bit messy compared to C but definitly workable. I will try that, and
look at the boost library.

Thanks,
Adam

Only if you only used the standard formatting. May I suggest you create
your own format function objects/classes and overloaded << >> stream
handling functions for your own objects/classes as required. The
resulting code is far more type safe and although it maybe it bit more
work to begin with, the flexible results are worth the effort. Not least
because all streamed I/O is encapsulated for later use.

JFJB
Nov 22 '05 #5

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

Similar topics

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...
10
by: ios | last post by:
Hi Can someone tell me what is different between below case? strcpy(eventname, "MDCX_RSP"); and sprintf(eventname, "MDCX_RSP"); Thanks, Leon
4
by: William Stacey [MVP] | last post by:
If val is a long, what is the String format conversion of this in c#? TIA! sprintf(retbuf,"%d.%.2d", val/100, val%100); -- William Stacey
10
by: Saurabh | last post by:
Hi all, I am working on RedHat Linux GCC 3.0. I am trying to convert a long to string through sprintf. but i m getting segmantation fault. I tried snprintf also but no avail.here is the piece...
66
by: gyan | last post by:
Hi All, I am using sprintf and getting starnge output in following case char temp_rn; memset(temp_rn,'\0',12); sprintf(temp_rn,"0%s",temp_rn); the final value in temp_rn is 00 how it...
2
by: jasonwthompson | last post by:
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. ...
173
by: Ron Ford | last post by:
I'm looking for a freeware c99 compiler for windows. I had intended to use MS's Visual C++ Express and use its C capability. In the past with my MS products, I've simply needed to make .c the...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.