Guest User

c++ large file

a guest
Feb 15th, 2012
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <stdint.h>
  2.  
  3. #include <cassert>
  4. #include <fstream>
  5. #include <iostream>
  6. #include <vector>
  7.  
  8. //! Main program that demonstrates large file handling
  9. int
  10. main
  11.    ( int const argc
  12.    , char const * const * const argv
  13.    )
  14. {
  15.    std::ofstream ofs("bigfile.bin", std::ios::binary);
  16.  
  17. std::cerr << sizeof(std::streamoff) << std::endl;
  18. std::cerr << sizeof(std::ios::pos_type) << std::endl;
  19. std::cerr << sizeof(std::ios::off_type) << std::endl;
  20.  
  21.    // 4mb test buffer
  22.    std::vector<uint32_t> buffer(1024 * 1024);
  23.  
  24.    size_t const bufbytes(sizeof(buffer[0]) * buffer.size());
  25.    std::generate(buffer.begin(), buffer.end(), std::rand);
  26.  
  27.    uint64_t totalbytes(0);
  28.  
  29.    uint64_t const desiredbytes(3ULL * 1024ULL * 1024ULL * 1024ULL);
  30.    while (totalbytes < desiredbytes)
  31.    {
  32.       ofs.write((char const *)&buffer[0], bufbytes);
  33.       totalbytes += bufbytes;
  34.    }
  35.  
  36.    ofs.close();
  37.  
  38.    std::ifstream ifs("bigfile.bin", std::ios::binary);
  39.    ifs.seekg(0, std::ios::end);
  40.  
  41.    std::ios::streampos const fsize(ifs.tellg());
  42.    std::cout << "seek from end: " << fsize << std::endl;
  43.  
  44.    ifs.seekg(0);
  45.    ifs.seekg(fsize);
  46.    std::cout << "seek to end: " << ifs.tellg() << std::endl;
  47.  
  48.    return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment