Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void reverse_file(const std::filesystem::path& path){
- static const size_t buffer_size = 8192;
- std::fstream file;
- file.open(path, std::ios::out | std::ios::in | std::ios::binary);
- file.seekg(0, std::ios_base::end);
- size_t size = file.tellg();
- size_t begin = 0;
- size_t end = size;
- char buffer[buffer_size * 2];
- while ( end - begin >= buffer_size * 2 ){
- file.seekg(begin, std::ios_base::beg);
- file.read(buffer, buffer_size);
- file.seekg(end - buffer_size, std::ios_base::beg);
- file.read(buffer + buffer_size, buffer_size);
- std::reverse(buffer, buffer + buffer_size);
- std::reverse(buffer + buffer_size, buffer + buffer_size * 2);
- file.seekp(begin, std::ios_base::beg);
- file.write(buffer + buffer_size, buffer_size);
- file.seekp(end - buffer_size, std::ios_base::beg);
- file.write(buffer, buffer_size);
- begin += buffer_size;
- end -= buffer_size;
- }
- size_t rest = end - begin;
- if (rest > 0 ){
- file.seekg(begin, std::ios_base::beg);
- file.read(buffer, rest);
- std::reverse(buffer, buffer + rest);
- file.seekp(begin, std::ios_base::beg);
- file.write(buffer, rest);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement