Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > From printf to C++ formatting

Reply
Thread Tools

From printf to C++ formatting

 
 
Steven T. Hatton
Guest
Posts: n/a
 
      08-01-2005
OK, I take back the nasty stuff I said about printf. At least until I see a
similarly concise way of writing this using C++ formatters. I want to
convert the following to something I can put onto a C++ std:stream:

printf( " [%2x] %-20s %-8.8s %08x %06x %02x %-3.3s %02x %04x %02x\n",
i,
std::string( pSec->GetName() ).substr( 0, 20 ).c_str(),
SectionTypes( pSec->GetType() ).c_str(),
pSec->GetAddress(),
pSec->GetSize(),
pSec->GetEntrySize(),
SectionFlags( pSec->GetFlags() ).c_str(),
pSec->GetLink(),
pSec->GetInfo(),
pSec->GetAddrAlign() );

One option I've looked at, and may come back to, is the Boost.Format
library. For now, I want to see if I can find a reasonably elegant way of
using the C++ format manipulators. Does anybody have a systematic approach
to this kind of situation?
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
 
Reply With Quote
 
 
 
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      08-02-2005

Steven T. Hatton wrote:
> OK, I take back the nasty stuff I said about printf. At least until I see a
> similarly concise way of writing this using C++ formatters. I want to
> convert the following to something I can put onto a C++ std:stream:
>
> printf( " [%2x] %-20s %-8.8s %08x %06x %02x %-3.3s %02x %04x %02x\n",
> i,
> std::string( pSec->GetName() ).substr( 0, 20 ).c_str(),
> SectionTypes( pSec->GetType() ).c_str(),
> pSec->GetAddress(),
> pSec->GetSize(),
> pSec->GetEntrySize(),
> SectionFlags( pSec->GetFlags() ).c_str(),
> pSec->GetLink(),
> pSec->GetInfo(),
> pSec->GetAddrAlign() );
>
> One option I've looked at, and may come back to, is the Boost.Format
> library. For now, I want to see if I can find a reasonably elegant way of
> using the C++ format manipulators. Does anybody have a systematic approach
> to this kind of situation?


With C++ io manipulators it would look ugly and much more error-prone
then a simple format string. The only trouble with printf is that it's
not type safe and it requires experience to know its pitfalls and use
it safely.

C++ standard streams and formatting are really clumsy.

 
Reply With Quote
 
 
 
 
Earl Purple
Guest
Posts: n/a
 
      08-02-2005

Maxim Yegorushkin wrote:
> With C++ io manipulators it would look ugly and much more error-prone
> then a simple format string. The only trouble with printf is that it's
> not type safe and it requires experience to know its pitfalls and use
> it safely.
>
> C++ standard streams and formatting are really clumsy.


That's why you can use boost::format which has the advantages of printf
and uses printf formatting, but also doesn't have the disadvantages of
the lack of type-safety and extensibility. (Extensibility in the type
of streams, not the type of data that can be given a formatting code).

(You can always extend your own objects to produce strings, of
different formats if required).

 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      08-02-2005

Steven T. Hatton wrote:
> OK, I take back the nasty stuff I said about printf. At least until I see a
> similarly concise way of writing this using C++ formatters. I want to
> convert the following to something I can put onto a C++ std:stream:
>
> printf( " [%2x] %-20s %-8.8s %08x %06x %02x %-3.3s %02x %04x %02x\n",
> i,
> std::string( pSec->GetName() ).substr( 0, 20 ).c_str(),
> SectionTypes( pSec->GetType() ).c_str(),
> pSec->GetAddress(),
> pSec->GetSize(),
> pSec->GetEntrySize(),
> SectionFlags( pSec->GetFlags() ).c_str(),
> pSec->GetLink(),
> pSec->GetInfo(),
> pSec->GetAddrAlign() );
>
> One option I've looked at, and may come back to, is the Boost.Format
> library. For now, I want to see if I can find a reasonably elegant way of
> using the C++ format manipulators. Does anybody have a systematic approach
> to this kind of situation?


In this particular instance though you have a lot of calls to pSec. Now
you might want to keep this a free function rather than a member
function, and maybe the class is not your own.

However it is inflexible as you can only output to the console or
wherever the console is redirected to. Also are you always going to
output to this particular format or are you going to modify it? And
remember type-safety - if, for example, you allow users to pick their
own format string so they can output in different formats, and their
format string doesn't comply with your function, then it will still
compile beautifully but could go "bang!" when you run it.

pSec is obviously a pointer to something, and it can't be NULL, and
presumably it only reads and the class it reads from is const-correct
so I'd change from a pointer to a const reference.

Next, if you really feel you must output then use sprintf - at least
here you know how big a buffer you need, so then you can get your
function to return what you have constructed converting it to a
std::string. (But I would go with boost::format).

Alternatively, your function could take an ostream& parameter and
return it. If the user wants a string they will use ostringstream. If
this is the only way you are ever going to format the object then you
may as well make this

std:stream & operator<<( std:stream &, const Section & );

Assuming Section is the name of your class. (As the first element i is
probably unrelated you would stream that in separately).

 
Reply With Quote
 
__PPS__
Guest
Posts: n/a
 
      08-02-2005
If I were you I'd use boost.format

here's how:
#include <boost/format.hpp>

class pSec_Class {
...
...
friend std:stream& opeartor<<
(stdstream& os, const pSec_Class &x){
static boost::format frmt("your %1% format %2% string .?"
"some other vars?..");
os << frmt % x.GetType().c_str() % x.GetSize(); // ...
return os;
}
};

....
std::cout << *pSec << std::endl;

 
Reply With Quote
 
Steven T. Hatton
Guest
Posts: n/a
 
      08-02-2005
__PPS__ wrote:

> If I were you I'd use boost.format
>
> here's how:
> #include <boost/format.hpp>
>
> class pSec_Class {
> ...
> ...
> friend std:stream& opeartor<<
> (stdstream& os, const pSec_Class &x){
> static boost::format frmt("your %1% format %2% string .?"
> "some other vars?..");
> os << frmt % x.GetType().c_str() % x.GetSize(); // ...
> return os;
> }
> };
>
> ...
> std::cout << *pSec << std::endl;


I probably should have worked through using the Boost.Format library. It
started looking a bit obscure when I tried to write it all out. What I
ended up doing was using brute force and C++ manipulators. It's not too
terribly ugly. I did create an operator<<() similar to what you show
above. Once I realized that there are two different things called `hex',
one being std::hex, and the other being ios_base::hex, and that one is a
value while the other is a function, things became much easier. That is a
gotch from hell. I couldn't figure out, for the life of me, why the
manipulator was not changing the format, but, instead, simply printing as a
numerical value.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
 
Reply With Quote
 
__PPS__
Guest
Posts: n/a
 
      08-02-2005
As far as I remember boost.format supports printf-like syntax. Probably
you can do like this:
static boost format frmt("printf-like string");
....
read docs for boost.format

 
Reply With Quote
 
__PPS__
Guest
Posts: n/a
 
 
Reply With Quote
 
Pierre Couderc
Guest
Posts: n/a
 
      08-04-2005
Steven T. Hatton a écrit :
> OK, I take back the nasty stuff I said about printf. At least until I see a
> similarly concise way of writing this using C++ formatters. I want to
> convert the following to something I can put onto a C++ std:stream:
>
> printf( " [%2x] %-20s %-8.8s %08x %06x %02x %-3.3s %02x %04x %02x\n",
> i,
> std::string( pSec->GetName() ).substr( 0, 20 ).c_str(),
> SectionTypes( pSec->GetType() ).c_str(),
> pSec->GetAddress(),
> pSec->GetSize(),
> pSec->GetEntrySize(),
> SectionFlags( pSec->GetFlags() ).c_str(),
> pSec->GetLink(),
> pSec->GetInfo(),
> pSec->GetAddrAlign() );
>
> One option I've looked at, and may come back to, is the Boost.Format
> library. For now, I want to see if I can find a reasonably elegant way of
> using the C++ format manipulators. Does anybody have a systematic approach
> to this kind of situation?


Is not your purpose vain?
Formatting has been well done once with printf specifications.
I do not think that C++ formatters are intended to replace printf
formatting, but to allow a quick not-so-well formatted output.
But if you want to well control printing, it is easier to turn back to
printf-like formatting.
Please note that it is what MS has done with its CString::Format()
Anyway, what seems to me missing is a bridge between printf-like
formatting and <string>.
 
Reply With Quote
 
Steven T. Hatton
Guest
Posts: n/a
 
      08-04-2005
Pierre Couderc wrote:

> Steven T. Hatton a écrit :
>> OK, I take back the nasty stuff I said about printf. At least until I
>> see a similarly concise way of writing this using C++ formatters. I want
>> to convert the following to something I can put onto a C++ std:stream:
>>
>> printf( " [%2x] %-20s %-8.8s %08x %06x %02x %-3.3s %02x %04x %02x\n",
>> i,
>> std::string( pSec->GetName() ).substr( 0, 20 ).c_str(),
>> SectionTypes( pSec->GetType() ).c_str(),
>> pSec->GetAddress(),
>> pSec->GetSize(),
>> pSec->GetEntrySize(),
>> SectionFlags( pSec->GetFlags() ).c_str(),
>> pSec->GetLink(),
>> pSec->GetInfo(),
>> pSec->GetAddrAlign() );
>>
>> One option I've looked at, and may come back to, is the Boost.Format
>> library. For now, I want to see if I can find a reasonably elegant way
>> of
>> using the C++ format manipulators. Does anybody have a systematic
>> approach to this kind of situation?

>
> Is not your purpose vain?
> Formatting has been well done once with printf specifications.
> I do not think that C++ formatters are intended to replace printf
> formatting, but to allow a quick not-so-well formatted output.
> But if you want to well control printing, it is easier to turn back to
> printf-like formatting.
> Please note that it is what MS has done with its CString::Format()
> Anyway, what seems to me missing is a bridge between printf-like
> formatting and <string>.


But how do you use printf with std:stream? The biggest obstacle I've
found with the C++ formatters is that there is no way that I am aware of to
truncate with them.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
Extracting printf(...) from (void) printf(....) guru Perl Misc 8 02-03-2009 10:37 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM
Re: printf-style formatting with streams. Joe Hotchkiss C++ 1 06-23-2003 10:53 PM



Advertisments