tranerius

25. Копирование файлов

Feb 4th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <Windows.h>
  5. class oneMB {
  6.     char empty_1[1000000];
  7. };
  8. int main() {
  9.     setlocale(LC_ALL, "ru");
  10.     std::fstream copyObject, pasteObject;
  11.     std::string copyFile, pasteFile, fileName;
  12.     long long int size, blockNumbers;
  13.     int temp;
  14.     std::cout << "Введите путь до файла и имя файла, который необходимо скопировать(с расширением): ";
  15.     SetConsoleCP(1251);
  16.     std::getline(std::cin, copyFile);
  17.     SetConsoleCP(866);
  18.     copyObject.open(copyFile, std::fstream::in | std::fstream::binary);
  19.     if (!copyObject.is_open()) {
  20.         std::cout << "Неверно указан путь или название файла" << std::endl;
  21.         system("pause");
  22.         return 0;
  23.     }
  24.     fileName = copyFile;
  25.     for (int i = 0, j=0; i < copyFile.size(); i++,j++) {
  26.         fileName[j] = copyFile[i];
  27.         if (copyFile[i] == '\\') {
  28.             j = -1;
  29.         }
  30.         temp = j;
  31.     }
  32.     fileName[temp+1] = '\0';
  33.     copyObject.seekg(0, std::ios::end);
  34.     size = copyObject.tellg();
  35.     copyObject.seekg(0, std::ios::beg);
  36.     if (size > 1000000) {
  37.         for (int i = 1000;; i++) {
  38.             if (size % i == 0) {
  39.                 blockNumbers = i;
  40.                 break;
  41.             }
  42.         }
  43.     }
  44.     else if (size > 1000 && size <= 10000) {
  45.         for (int i = 10;; i++) {
  46.             if (size % i == 0) {
  47.                 blockNumbers = i;
  48.                 break;
  49.             }
  50.         }
  51.     }
  52.     else if (size > 10000 && size <= 100000) {
  53.         for (int i = 100;; i++) {
  54.             if (size % i == 0) {
  55.                 blockNumbers = i;
  56.                 break;
  57.             }
  58.         }
  59.     }
  60.     else {
  61.         blockNumbers = 1;
  62.     }
  63.     int val = size / blockNumbers;
  64.     std::cout << "Введите путь, куда необходимо скопировать файл: ";
  65.     SetConsoleCP(1251);
  66.     std::getline(std::cin, pasteFile);
  67.     SetConsoleCP(866);
  68.     if (pasteFile == copyFile) {
  69.         std::cout << "Одинаковые директории и названия файлов." << std::endl;
  70.         system("pause");
  71.         return 0;
  72.     }
  73.     if (pasteFile[pasteFile.size()] != '\\') {
  74.         pasteFile += '\\';
  75.     }
  76. pasteFile = pasteFile + fileName;
  77. pasteObject.open(pasteFile, std::fstream::out | std::fstream::app | std::fstream::binary);
  78. if (!pasteObject.is_open()) {
  79.     std::cout << "Ошибка при вводе директории" << std::endl;
  80.     system("pause");
  81.     return 0;
  82. }
  83. oneMB obj;
  84. int onePartOfFile = blockNumbers / 100;
  85. int partOfFile = onePartOfFile;
  86. int count = 0;
  87. for (int i = 0; i < blockNumbers; i++) {
  88.     if (blockNumbers != 1) {
  89.         if (i == 0 || i == partOfFile) {
  90.             system("cls");
  91.             std::cout << "Процесс копирования завершен на " << count++ << "%";
  92.             if (count > 100) {
  93.                 count = 100;
  94.             }
  95.             partOfFile += onePartOfFile;
  96.         }
  97.     }
  98.     else {
  99.         std::cout << "100%";
  100.     }
  101.     copyObject.read((char*)&obj, size / blockNumbers);
  102.     pasteObject.write((char*)&obj, size / blockNumbers);
  103. }
  104. system("cls");
  105. std::cout << "Процесс копирования завершен на 100%" << std::endl;
  106. copyObject.close();
  107. pasteObject.close();
  108. system("pause");
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment