【问题标题】:Need to read a text file of size greater than 5 GB in chunks to reduce processing time using C++需要分块读取大于 5 GB 的文本文件以减少使用 C++ 的处理时间
【发布时间】:2014-11-21 06:38:02
【问题描述】:
#include<iostream>
using namespace std;
int main(){    
    string lineValue;
    ifstream myFile("file.txt");
    if (myFile.is_open()) {

        while (getline(myFile, lineValue)) {
            cout << lineValue << '\n';
        } 
        myFile.close();
    }
    else cout << "Unable to open file";
    return 0;
}
  1. 想要分块读取文件。
  2. 我刚刚读取的块应该进一步解析。
  3. 同时应该读取另一个块。

我应该怎么做?

【问题讨论】:

  • 该代码只是一个“逐行读取文件”的通用示例,并没有记录您在多线程领域投入的任何想法。
  • 您可能想要查找“双缓冲”(或 'multiple buffering'),因为这似乎是您想要做的。

标签: c++ fstream chunks


【解决方案1】:

可以在Message passing between threads using a command file 下找到对此的一种解决方案(逐行读取文件并将这些行传递给线程)。然而,效率取决于工作线程任务的复杂性。有时文件 I/O 缓冲的效率已经足以补偿 I/O 延迟。

【讨论】:

    猜你喜欢
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 2021-12-11
    • 1970-01-01
    • 2019-03-12
    相关资源
    最近更新 更多