文件输出中的有限值

limited value in a File output

本文关键字:输出 文件      更新时间:2023-10-16

我有一个返回执行时间的程序,每次运行的输出显示在同一行的文件中。

#include <iostream>
#include <fstream>
#include <math>
#include <boost>
using namespace std;
int main() {
std::ofstream file("MyFile.txt",std::ios_base::app);
v2 = rand() % 100 + 1; // just to burn  some time 
std::cout << v2 ;    // just to burn  some time

boost::chrono::duration<double> sec = boost::chrono::system_clock::now() - start;
    file<<sec.count() << ";";
file.close();
  return 0 ;
}

4运行后,文件中的输出如下:

0.0190567;4.92035;11.0541;13.1457;

我希望在30次运行后,第30次结果必须以换行符显示。没有任何增加的函数或循环,这将影响运行时的持续时间。我认为还有其他的方法来格式化输出文件通过awk sed。

您可以使用awk。

awk '{ gsub(/([0-9]+(.[0-9]*)?;){30}/, "&n"); print }' MyFile.txt

这应该在每30个;之后插入一个n