read large files part by part in C++

#include <iostream>
#include <fstream>

int main() {
    const std::string filename = "large_file.txt";
    const std::streamsize chunk_size = 1024;  // Adjust the chunk size as needed

    std::ifstream file(filename, std::ios::binary);

    if (!file.is_open()) {
        std::cerr << "Error opening file." << std::endl;
        return 1;
    }

    char buffer[chunk_size];

    while (file.read(buffer, chunk_size)) {
        // Process the current chunk here
        // Use 'buffer' to access the data in the chunk
    }

    // The last chunk may be smaller than chunk_size
    std::streamsize last_chunk_size = file.gcount();
    if (last_chunk_size > 0) {
        // Process the last chunk here
        // Use 'buffer' to access the data in the last chunk
    }

    file.close();

    return 0;
}