Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

read large files part by part in C++

// 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;
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #read #large #files #part #part
ADD COMMENT
Topic
Name
9+9 =