2013-01-13 56 views
0

我想输出C++程序运行到桌面上名为C10.txt的文件所需的时间。我想运行该程序100次,然后当我打开文件时,它会告诉我程序运行的所有不同时间。我知道我需要fstream来做到这一点,但我不知道该从哪里去。这是我想多次运行的代码。我应该添加什么来完成这项工作?通过多次运行程序以C++多次输出文件

#include <iostream> 
#include <cmath> 
#include <fstream> 

using namespace std; 

void merge(int*,int,int); 
void mergeSort(int*,int,int); 


const int ARRAYSIZE = 10; 

int main() 
{ 

    clock_t startTime = clock(); 
    srand(unsigned(time(0))); 

    cout << (double(clock() - startTime)/(double)CLOCKS_PER_SEC) * 1000000<< " microseconds." << endl; 


    return 0; 
} 

回答

0

在实际执行任何操作之前,在main()中创建一个ofstream yournamehere ("path_to_desktop\C10.txt");

然后,只需在for循环中调用您的代码,将clock()分配给开始时的startTime,并将您正在编写的内容写入coutyournamehere

+0

首先,谢谢你的帮助!其次,使用上面的代码,我现在可以将时间添加到C10.txt文档中,但每次运行该程序时,C10.txt文档中的时间都会更改。我希望每次运行程序时,新的时间都会被添加到C10.txt文件 – Mayankmmmx

+0

'ofstream yournamehere(“path_to_desktop \ C10.txt”,ios :: app);'那么就是。 –

0

以追加模式打开ofstream。 std::ofstream tlog("C10.txt", std::ofstream::app);。 主要将时钟记录写入tlog而不是std::cout

您使用std::endl是浪费。 Endl冲洗流,听起来不像你需要的那样。只需直接使用换行符“\ n”即可。