I’m trying to write numeric values into a text file aligned to columns. My code looks like this:
ofstream file;
file.open("try.txt", ios::app);
file << num << "\t" << max << "\t" << mean << "\t << a << "\n";
It works, except if the values don’t have the same number of digits, they don’t align. What I would like is the following:
1.234567 -> 1.234
1.234 -> 1.234
1.2 -> 1.200
It depends on what format you want. For a fixed decimal place,
something like:
should do the trick, so you can write:
(or whatever width and precision you want).
If you’re doing any floating point work at all, you should probably have
such a manipulator in your took kit (although in many cases, it will be
more appropriate to use a logical manipulator, named after the logical
meaning of the data it formats, e.g. degree, distances, etc.).
IMHO, it’s also worth extending the manipulators so that they save the
formatting state, and restore it at the end of the full expression.
(All of my manipulators derive from a base class which handles this.)