Advertisement
tinyevil

Untitled

Jul 31st, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. void reverse_file(const std::filesystem::path& path){
  2.     static const size_t buffer_size = 8192;
  3.  
  4.     std::fstream file;
  5.     file.open(path, std::ios::out | std::ios::in | std::ios::binary);
  6.     file.seekg(0, std::ios_base::end);
  7.     size_t size = file.tellg();
  8.  
  9.     size_t begin = 0;
  10.     size_t end = size;
  11.    
  12.     char buffer[buffer_size * 2];
  13.  
  14.     while ( end - begin >= buffer_size * 2 ){
  15.         file.seekg(begin, std::ios_base::beg);
  16.         file.read(buffer, buffer_size);
  17.         file.seekg(end - buffer_size, std::ios_base::beg);
  18.         file.read(buffer + buffer_size, buffer_size);
  19.  
  20.         std::reverse(buffer, buffer + buffer_size);
  21.         std::reverse(buffer + buffer_size, buffer + buffer_size * 2);
  22.  
  23.         file.seekp(begin, std::ios_base::beg);
  24.         file.write(buffer + buffer_size, buffer_size);
  25.         file.seekp(end - buffer_size, std::ios_base::beg);
  26.         file.write(buffer, buffer_size);
  27.  
  28.         begin += buffer_size;
  29.         end -= buffer_size;
  30.     }
  31.  
  32.     size_t rest = end - begin;
  33.     if (rest > 0 ){    
  34.         file.seekg(begin, std::ios_base::beg);
  35.         file.read(buffer, rest);
  36.         std::reverse(buffer, buffer + rest);
  37.         file.seekp(begin, std::ios_base::beg);
  38.         file.write(buffer, rest);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement