Default Precision Setting Method for All Streams

Hey. I am trying to figure out if there is a way to set the default precision for streams. To clarify, I can set the precision of the stream, which I determined without issue. I just can't find a way to do this, so when I create a stream it has this default precision. Thanks in advance.

+3


source to share


1 answer


Perhaps getting a custom version ofstream

that sets the precision in its constructor:



#include <fstream>

struct my_ofstream : std::ofstream {
    explicit my_ofstream(std::streamsize prec = 5)
    {
        this->precision(prec);
    }
};

int main()
{
   my_ofstream f1;  // default precision 5
   my_ofstream f2(10);
}

      

+3


source







All Articles