在 C++ 中重复读取和写入同一文件

Reading and writing the same file repeatedly in C++

提问人:Lena Abbasi 提问时间:4/7/2023 最后编辑:Lena Abbasi 更新时间:4/7/2023 访问量:56

问:

如果这个问题太基本,我深表歉意。我正在尝试通过 fstream 重复读取和写入 12 个文件。这是为了衡量它所花费的时间并平均它。但我似乎在这里重置 EOF 有问题。我尝试过 clear() 和 seekg(0),但没有运气。任何意见都是值得赞赏的: 从 https://stackoverflow.com/a/11564931

// From https://stackoverflow.com/a/11564931


#define _CRT_SECURE_NO_WARNINGS

#include <fstream>
#include <chrono>
#include <vector>
#include <cstdint>
#include <numeric>
#include <random>
#include <algorithm>
#include <iostream>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;


long long fileCopy(ifstream & input,ofstream & output)
{
  char c;
  string line;
  auto startTime = std::chrono::high_resolution_clock::now();
  if (input.is_open() && output.is_open())
  {
    while (!input.eof()) 
    {
        getline(input, line);
        output << line << endl;
    }
   }
  else
   {
    cout << "Unable to open input or output file\n";
   }
input.clear();
input.seekg(0,ios::beg);
output.clear();
output.seekp( 0 );
input.close();
output.close();
auto endTime = std::chrono::high_resolution_clock::now();
return chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
}

int main()
{

    long long totalTime = 0.0;

    ifstream inputFile ("input.txt" );
    ifstream inputFile1 ("input1.txt" );
    ifstream inputFile2 ("input2.txt" );
    ifstream inputFile3 ("input3.txt" );
    ifstream inputFile4 ("input4.txt" );
    ifstream inputFile5 ("input5.txt" );
    ifstream inputFile6 ("input6.txt" );
    ifstream inputFile7 ("input7.txt" );
    ifstream inputFile8 ("input8.txt" );
    ifstream inputFile9 ("input9.txt" );
    ifstream inputFile10 ("input10.txt" );
    ifstream inputFile11 ("input11.txt" );
    ifstream inputFile12 ("input12.txt" );

    ofstream outputFile("output.txt");
    ofstream outputFile1("output1.txt");
    ofstream outputFile2("output2.txt");
    ofstream outputFile3("output3.txt");
    ofstream outputFile4("output4.txt");
    ofstream outputFile5("output5.txt");
    ofstream outputFile6("output6.txt");
    ofstream outputFile7("output7.txt");
    ofstream outputFile8("output8.txt");
    ofstream outputFile9("output9.txt");
    ofstream outputFile10("output10.txt");
    ofstream outputFile11("output11.txt");
    ofstream outputFile12("output12.txt");


    //1000 times doing small size read and write
    for (size_t i = 0; i < 1000; i++)
    {
        totalTime += fileCopy(inputFile1,outputFile1);
        
        totalTime += fileCopy(inputFile2,outputFile2);
        totalTime += fileCopy(inputFile3,outputFile3);
        totalTime += fileCopy(inputFile4,outputFile4);
        totalTime += fileCopy(inputFile5,outputFile5);
        totalTime += fileCopy(inputFile6,outputFile6);
        totalTime += fileCopy(inputFile7,outputFile7);
        totalTime += fileCopy(inputFile8,outputFile8);
        totalTime += fileCopy(inputFile9,outputFile9);
        totalTime += fileCopy(inputFile10,outputFile10);
        totalTime += fileCopy(inputFile11,outputFile11);
        totalTime += fileCopy(inputFile12,outputFile12);
       
    }
    
    //cout<< "The big file copy timing is "<< fileCopy(inputFile,outputFile) << " ms " << endl;
    cout<< "Total time to do 1000 times copying the 12 small files is "<< totalTime << " ms " << endl;
    return 0;
}

我尝试了 input.clear() 和 input.seekg(0) 以及 output.clear() 和 output.seekp(0)。没有运气。

C++ EOF

评论

2赞 Martin York 4/7/2023
你文件。但你永远不会重新打开它们。close()
2赞 Martin York 4/7/2023
这是一种反模式。请参阅为什么循环条件中的 iostream::eof 被认为是错误的?while (!input.eof())
1赞 Martin York 4/7/2023
endTime在您关闭并重置流后被记录下来!您想将重置文件作为您时间的一部分吗?
0赞 Lena Abbasi 4/7/2023
谢谢!我完全错过了......唉。。
1赞 john 4/7/2023
你真的不需要,在你即将要的文件中.关闭流,然后重新打开它是重用流的最干净的方法。clearseekpclose

答: 暂无答案