Skip to content

Instantly share code, notes, and snippets.

@spot62
Last active June 8, 2018 21:21
Show Gist options
  • Save spot62/f990a92fad938c2fd1a80c50565c78c1 to your computer and use it in GitHub Desktop.
Save spot62/f990a92fad938c2fd1a80c50565c78c1 to your computer and use it in GitHub Desktop.
Read a file by chunks
#include <iostream>
#include <fstream>
#include <vector>
char* filename = "test.bin";
size_t chunk_sz = (32*1024);
int main(void)
{
std::ifstream fin(filename, std::ifstream::ate | std::ifstream::binary);
if(fin.is_open())
{
size_t offset = 0;
size_t chunk_len = 0;
size_t total_size = fin.tellg();
std::vector<char> chunk(chunk_sz, 0);
fin.clear();
fin.seekg(0, std::ios::beg);
while (!fin.eof())
{
fin.read(chunk.data(), chunk.size());
chunk_len = fin.gcount();
std::cout << "input file position: " << offset << "+" << chunk_len <<" of " << total_size << std::endl;
offset += fin.gcount();
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment