gpt4 book ai didi

c++ - 将缓冲的文本数据读入字符串

转载 作者:太空宇宙 更新时间:2023-11-04 12:20:14 25 4
gpt4 key购买 nike

您好,我想从挂起的文本文件中直接读取数据到字符串我知道我可以使用以下内容:

ifstream infile("myfile");
string str(istreambuf_iterator<char>(infile), istreambuf_iterator<char>());

但是这种方式一步读取整个文件。我想分几步阅读它,因为这是一个大约 50GB 的非常大的文件。我该怎么做?感谢 Herzl 的建议。

最佳答案

我会做这样的事情(可以修改 bufSize 以满足您的需要):

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void ReadPartially(const char *file)
{
const unsigned bufSize = 10;
ifstream is(file);
string str(bufSize, '\0');
unsigned count=0;
while(!is.eof())
{
count++;
is.read(&str[0], bufSize);
cout << "Chunk " << count << ":[" << endl << str << "]" << endl;
}
}

关于c++ - 将缓冲的文本数据读入字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5485346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com