Are there good examples of good C++ I/O usage
Question
I am heavily involved in doing I/O in C++ (currently using it for printing headers, tables, some data alignments), and wonder about it proper/great usage in either open source projects or general examples/nippets
I use things such:
cout.setf(ios::right,ios::jyustified);
cout<<std::setw()
std::copy (vector.begin(), vector.end(), std::osteam_iterator<const Foo *>
std::cout,"\n"); //provided I have operator << in/for Foo
locale mylocale("");
cout.imbue( mylocale );
I don't like my current implementation, as I have a lot of forced (\t)
and spaces
to ensure correct indentation. Hence I want to see how I/O is used by top notch professionals.
Solution
One thing that's extremely useful is the Boost IO state saver library. This provides (in particular) a clean way to deal with "sticky" flags.
I tend to agree with David Seiler though -- very few people will be happy with output that's pure text in a single font, etc., that you get by writing just text. HTML, however, is quite easy to generate. RAII, however, can work nicely with fixed-structure documents to ensure that you always generate properly nested tags.
OTHER TIPS
If you want to format data in some easy-to-read way, consider emitting some kind of structured document format (e.g. html or pdf) instead of pure text-plus-spaces-and-tabs. That problem isn't C++ I/O specific, and can't really be solved with clever use of <stdio>
.
For the indentation part of your question: The concept of "filter" (as implemented, for example, in the Boost.IOStreams library) is pretty useful and powerful in this respect. For an example, see this answer.