流复位精度

and*_*rea 5 c++ precision ofstream

我正在使用c ++来操作txt文件.我需要写一些具有一定精度的数字,所以我在做:

    ofstrem file;
    file.open(filename, ios::app);
    file.precision(6);
    file.setf(ios::fixed, ios::floafield);
    //writing number on the file.....
Run Code Online (Sandbox Code Playgroud)

现在我需要写其他东西,所以我需要重置精度.我该怎么做?

Lig*_*ica 7

首先检索流的原始精度值precision(),存储,更改,插入,然后将其更改回存储的值.

int main() {
   std::stringstream ss;
   ss << 1.12345 << " ";

   std::streamsize p = ss.precision();

   ss.precision(2);
   ss << 1.12345 << " ";

   ss.precision(p);
   ss << 1.12345;

   cout << ss.str();  // 1.12345 1.1 1.12345
}
Run Code Online (Sandbox Code Playgroud)

现场演示.