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 ?
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 ?
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.
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).
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.
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).
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.