views:

280

answers:

5

Hi,

I don't like to use <iostream> in C++ Library. I prefer to use something that is similar to "printf" and "scanf" in <stdio.h>.

Can I use Boost's format library to replace <iostream> in all my C++ program ?

+2  A: 

You can continue to use printf in C++, nothing wrong with that. Just #include <cstdio> and you're good to go. Of course, iostream has several benefits (type-safety being the big one) - so I'd still recommend switching.

tzaman
From what I've heard, <cstdio> is same like <stdio.h>, it is not type-safe. I want something that is as good as <iostream>, which is type-safe, but offer something similar to "printf" and "scanf". Thanks.
CMW
`nothing wrong with that`: Isn't lack of type safety something wrong with printf? :P
Bill
I don't think type-safety is that bit a deal here. As long as the first argument to `printf` is a literal, most compilers will warn about potential type violations.
Marcelo Cantos
@Marcelo: Thank you. Can I say that I can continue to use <cstdio>/<stdio.h> in all my C++ program without any problem ?
CMW
@CMW: Yes, but there are minor hazards. stdio and iostream may not synchronise buffers between each other, so you could find that things print out of order.
Marcelo Cantos
@Marcelo Cantos: Unless you use `sync_with_stdio`.
Billy ONeal
Ah yes, I forgot that little nugget. Thanks Billy.
Marcelo Cantos
+4  A: 

Boost Format only does the formatting bit. You still need iostream to actually make something appear on the screen.

Of course, using them together will achieve the parity with printf you are looking for. And it does so without sacrificing type-safety (though that's not a huge issue these days, since the compiler will usually warn about bad printf arguments).

Marcelo Cantos
I'm sorry but I don't know about this. Can I say that if I use <iostream> + Boost Format Library, then I can get the features I want (similar to <stdio.h> but type-safe)? Thanks.
CMW
@CMW Yes, this is exactly the goal of Boost.Format.
Vicente Botet Escriba
@Vicente: Thank you. But unfortunately Boost.Format doesn't work with istream.
CMW
+3  A: 

Boost.Format works with ostreams. You use it as follows

cout << boost::format("%2% %1%") % 36 % 77; 

or like

string s2 = str( format("%2% %1%") % 36 % 77 );

and the print the string as you want.

Boost.Format doesn't works with istreams.

Vicente Botet Escriba
Thank you, Vicente.
CMW
A: 

printf and scanf are limited in the way you cannot use your own objects with them.

If you're using C++, you'll need to know a bit about streams, because that's the standard of the language. In C++ you can create stream operators for your own objects and have them work perfectly with streams.

Of course, you can use boost::format, but it will only be for formatted output. I like boost::format, and it's very useful, when you're using localisation for instance (translatable strings).

Nikko
Thank you, Nikko.
CMW
A: 

The short answer, as given by others, is yes, but there is no equivalent for scanf in Boost-format. However, an alternative is to use Boost's Spirit library which has both input and output capabilities via the Karma and Qi components, respectively. This may very well be overkill for what you are doing, though, as it is a full parser/generator implementation.

rcollyer
Thank you, rcollyer.
CMW