📜  在 C++ 代码示例中逐部分读取大文件

📅  最后修改于: 2022-03-11 14:44:57.279000             🧑  作者: Mango

代码示例1
// 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;