C++ C++;逐部分读取大文件
我一直有一个问题,到目前为止我还不能解决。这个问题与读取文件有关,我甚至在这个网站上看过线程,但它们似乎并没有解决这个问题。这个问题是读取比计算机系统内存大的文件。刚才当我问这个问题时,我也使用了下面的代码C++ C++;逐部分读取大文件,c++,C++,我一直有一个问题,到目前为止我还不能解决。这个问题与读取文件有关,我甚至在这个网站上看过线程,但它们似乎并没有解决这个问题。这个问题是读取比计算机系统内存大的文件。刚才当我问这个问题时,我也使用了下面的代码 string data(""); getline(cin,data); std::ifstream is (data);//, std::ifstream::binary); if (is) { // get length of file: is.seekg (0,
string data("");
getline(cin,data);
std::ifstream is (data);//, std::ifstream::binary);
if (is)
{
// get length of file:
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
// allocate memory:
char * buffer = new char [length];
// read data as a block:
is.read (buffer,length);
is.close();
// print content:
std::cout.write (buffer,length);
delete[] buffer;
}
system("pause");
这段代码运行良好,除了它像糖果店的胖子一样消耗内存这一事实。
所以在经历了大量的贫民区和粗制滥造的编程之后,我找到了一种解决问题的方法。然而,在我的探索中,我或多或少地把一个问题换成了另一个问题
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <windows.h>
#include <cstdlib>
#include <thread>
using namespace std;
/*======================================================*/
string *fileName = new string("tldr");
char data[36];
int filePos(0); // The pos of the file
int tmSize(0); // The total size of the file
int split(32);
char buff;
int DNum(0);
/*======================================================*/
int getFileSize(std::string filename) // path to file
{
FILE *p_file = NULL;
p_file = fopen(filename.c_str(),"rb");
fseek(p_file,0,SEEK_END);
int size = ftell(p_file);
fclose(p_file);
return size;
}
void fs()
{
tmSize = getFileSize(*fileName);
int AX(0);
ifstream fileIn;
fileIn.open(*fileName, ios::in | ios::binary);
int n1,n2,n3;
n1 = tmSize / 32;
// Does the processing
while(filePos != tmSize)
{
fileIn.seekg(filePos,ios_base::beg);
buff = fileIn.get();
// To take into account small files
if(tmSize < 32)
{
int Count(0);
char MT[40];
if(Count != tmSize)
{
MT[Count] = buff;
cout << MT[Count];// << endl;
Count++;
}
}
// Anything larger than 32
else
{
if(AX != split)
{
data[AX] = buff;
AX++;
if(AX == split)
{
AX = 0;
}
}
}
filePos++;
}
int tz(0);
filePos = filePos - 12;
while(tz != 2)
{
fileIn.seekg(filePos,ios_base::beg);
buff = fileIn.get();
data[tz] = buff;
tz++;
filePos++;
}
fileIn.close();
}
void main ()
{
fs();
cout << tmSize << endl;
system("pause");
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
/*======================================================*/
字符串*文件名=新字符串(“tldr”);
字符数据[36];
int filePos(0);//文件的位置
int tmSize(0);//文件的总大小
int-split(32);
半焦;
int-DNum(0);
/*======================================================*/
int getFileSize(std::string filename)//文件路径
{
FILE*p_FILE=NULL;
p_file=fopen(filename.c_str(),“rb”);
fseek(p_文件,0,SEEK_结束);
int size=ftell(p_文件);
fclose(p_文件);
返回大小;
}
void fs()
{
tmSize=getFileSize(*文件名);
int AX(0);
ifstreamfilein;
fileIn.open(*文件名,ios::in | ios::binary);
int n1、n2、n3;
n1=tmSize/32;
//是否进行处理
while(filePos!=tmSize)
{
fileIn.seekg(filePos,ios_base::beg);
buff=fileIn.get();
//考虑小文件
如果(tmSize<32)
{
整数计数(0);
char-MT[40];
如果(计数!=tmSize)
{
MT[计数]=增益;
cout如果您不需要同时使用所有文件内容,请首先减少工作集,例如大约32个字的工作集,但由于XOR可以按顺序应用,因此可以进一步简化工作集,使其具有恒定的大小,例如4 KB
现在,您可以选择在循环中使用文件读取器is.read()
,并在每次迭代中处理一小部分数据,或者使用memmap()
将文件内容映射为内存指针,您可以执行读写操作。要逐段读取和处理文件,可以使用以下代码段:
// Buffer size 1 Megabyte (or any number you like)
size_t buffer_size = 1<<20;
char *buffer = new char[buffer_size];
std::ifstream fin("input.dat");
while (fin)
{
// Try to read next chunk of data
fin.read(buffer, buffer_size);
// Get the number of bytes actually read
size_t count = fin.gcount();
// If nothing has been read, break
if (!count)
break;
// Do whatever you need with first count bytes in the buffer
// ...
}
delete[] buffer;
//缓冲区大小为1 MB(或您喜欢的任何数字)
size\u t buffer\u size=1我对使用xor有点困惑。这是怎么进入故事的?我也没有发现你在你的代码中实际使用xor的地方。我想稍后在这个程序中使用xor,但为了更好地理解文件读取器的一些问题,我没有这样做。不过,我不打算限制这段代码的使用就Xor而言,它或多或少也是一个可以应用的例子。我为没有说得更清楚而道歉。如果我在任何回复或短信中听起来很粗鲁,我也为此道歉,我只是想说得非常清楚。我从来没有意识到糖果店的胖孩子以吃记忆而闻名。嗯……我没有领会你的意思。第一副总裁de-example看起来很糟糕。“[…]读取比计算机系统内存大的文件。[code]这段代码工作得很好,除了它像糖果店的胖子一样消耗内存这一事实。“我的意思是它根本不工作。getline(cin,数据);
将整个文件读取到内存中。而您的第二个代码似乎过于复杂,仅用于将文件分块读取。@LightnessRacesinOrbit Point,这太愚蠢了,我决定不编辑注释,并承担羞耻=)谢谢,我可以尝试后一种方式,但在代码形式上如何。它对常规文本非常有效,但它在某些二进制文件(如exe)上完全不起作用。我很确定它应该可以正常工作。你能详细说明它是如何失败的吗?也许问题在于处理过程中?如果你读取类似xor的数据,它只会读取其中非常小的一部分。同样适用于exe'sWell,我自己刚刚尝试过代码,我可以读取没有任何问题的可执行文件。您使用的是什么操作系统和编译器?可能您必须为流构造函数.outFile.write(buffer,count)指定ios::binary
标志;我解决了这个问题,现在唯一的问题是对缓冲区执行多次操作,这样它会影响所有数据。