console output formatting
I am sorry, but I need a quick help
what would be analog to this function in C to C++
printf("%5d", var)
cout << var ?????????
but how to format it to take 5 places in console ?
what would be analog to this function in C to C++
printf("%5d", var)
cout << var ?????????
but how to format it to take 5 places in console ?
printf("%5d", var)
will work in C++. Most C functions, at least all C functions i know, work in C++you must include <cstdio> and it works printf("%5d", var);
Last edited on Jun 11, 2010 at 8:55pm
Neither answers his question, however...
To do that in C++, #include <iomanip> and use the following construct:
Hope this helps.
To do that in C++, #include <iomanip> and use the following construct:
cout << setw( 5 ) << var;
Hope this helps.
This reluctance to adopt C++ constructs is far too common. It's a pet peeve of mine.
Honestly, I still think printf/etc is easier (as in less typing, easier to remember the codes) than the iostream counterparts. Especially since iostream doesn't seem to remember the last modifiers you give it.
The iostream approach is even more horrifying if you you're not using the "using" directive.
I'm not discounting the merits of iostream. I know that it's better in a lot of ways (and why). I'm just showing why so many people are still reluctant to use it.
|
|
The iostream approach is even more horrifying if you you're not using the "using" directive.
I'm not discounting the merits of iostream. I know that it's better in a lot of ways (and why). I'm just showing why so many people are still reluctant to use it.
Last edited on Jun 12, 2010 at 12:15am
I disagree.
The reason people use C stuff over C++ stuff is because they have already been contaminated with C.
The C counterpart doesn't remember the last modifier you give it either.
The only thing that can be said against iostream is that it is more verbose...
The reason people use C stuff over C++ stuff is because they have already been contaminated with C.
The C counterpart doesn't remember the last modifier you give it either.
The only thing that can be said against iostream is that it is more verbose...
Wait, I never learnt C and I still prefer cstdio over iostream in some situations. Terseness is one reason, but the other is that "%08X" is much easier to remember than <<std::setw(8)<<std::setfill('0')<<std::hex. I often find it preferable to just sprintf() to an array than go look up voodoo incantations. Sure, it's laziness, but what exactly am I loosing, other than consistency?
It might be nice to have a manipulator to configure the stream output according to cstdio formatting rules:
|
|
Duoas wrote: |
---|
The C counterpart doesn't remember the last modifier you give it either. |
But repeating the format for the C counterpart doesn't involve a full line of text. It's just 4-6 characters tops.
Duaos wrote: |
---|
The only thing that can be said against iostream is that it is more verbose... |
You say that like it's insignificant. For some things I guess it's not a big deal, but for writing trace logs and stuff where you're dumping lots of data, it's quite a big deal.
Galik wrote: |
---|
It might be nice to have a manipulator to configure the stream output according to cstdio formatting rules: |
I've thought about that more than once. The thing is it kind of defeats half the point of using iostream.
cstdio's 2 big problems are:
- type safety
- having to parse a format string
By having a cformat() manipulator you're reintroducing half of the problems iostream solves.
Plus, I don't think there's an iostream equivilent of %g, but I might be wrong on that.
Another idea would be to make modifiers for common output formats. Like:
|
|
But that would require you to write lots of modifiers.
'SYSTEM PROGRAMMING' 카테고리의 다른 글
race condition (0) | 2015.08.16 |
---|---|
서브넷 마스크에 대한 문제 (0) | 2015.08.12 |
IP 주소로의 여행 (0) | 2015.08.11 |
경계적 대기 - win32 multihthreading (0) | 2015.08.11 |
기초적 쓰레드 동기화 (0) | 2015.08.10 |