c++逐部分读取大文件

C++ reading large files part by part

本文关键字:文件 读取 逐部 c++      更新时间:2023-10-16

我一直有一个问题,我还没有能够解决。这个问题与阅读文件有关,我甚至在这个网站上看过线程,他们似乎没有解决这个问题。这个问题是读取比计算机系统内存还大的文件。当我刚才问这个问题的时候,我也使用了下面的代码。

    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");
}

我试图用这段代码做的是围绕内存问题的工作。我没有为一个在我的系统中根本不存在的大文件分配内存,而是尝试使用我拥有的大约8gb的内存,但如果可能的话,我只想使用几kb。为了让你们对我所讲的内容有一个大致的了解,我将写一行文字。"你好,我叫蛋糕,请给我蛋糕。"基本上我所做的就是逐字逐句地读这篇文章。然后我把这些字母放进一个可以存储32个字母的盒子里,从那里我可以使用xor之类的东西,然后把它们写到另一个文件里。这个想法在某种程度上是有效的,但它非常慢,并且会遗漏部分文件。所以基本上,我怎样才能使这样的东西工作,而不会变慢或切断文件。我很想看看xor如何处理非常大的文件。因此,如果有人有比我更好的想法,那么我将非常感谢你的帮助。

要逐条读取和处理文件,可以使用以下代码片段:

// 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;

32字节的缓冲区大小,正如您所使用的,肯定是太小了。您对库函数进行了太多的调用(而库反过来又对操作系统进行调用(尽管可能不是每次),这通常很慢,因为它们会导致上下文切换)。也不需要tell/seek。

如果您不需要同时使用所有的文件内容,请先减少工作集-例如大约32个单词的集合,但是由于异或可以按顺序应用,您可以使用固定大小进一步简化工作集,例如4千字节。

现在,您可以选择在循环中使用文件读取器is.read()并在每次迭代中处理一小部分数据,或者使用memmap()将文件内容映射为可以执行读和写操作的内存指针。