tranerius

создание мусора

Mar 5th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. enum encode_mode {
  7.     UTFtoWIN,
  8.     WINtoUTF
  9. };
  10.  
  11. std::string encoder(std::string text_to_encode, int encode_mode) {
  12.     BSTR bstrWide;
  13.     int length;
  14.     char *text = new char[text_to_encode.size() + 1];
  15.     for (int i = 0; i < text_to_encode.size(); i++) {
  16.         text[i] = text_to_encode[i];
  17.     }
  18.     text[text_to_encode.size()] = '\0';
  19.     if (encode_mode == encode_mode::UTFtoWIN) {
  20.         length = MultiByteToWideChar(CP_UTF8, 0, text, strlen(text), NULL, NULL);
  21.         bstrWide = SysAllocStringLen(NULL, length);
  22.         MultiByteToWideChar(CP_UTF8, 0, text, strlen(text), bstrWide, length);
  23.         length = WideCharToMultiByte(1251, 0, bstrWide, -1, NULL, 0, NULL, NULL);
  24.         char *text_1251 = new char[length];
  25.         WideCharToMultiByte(1251, 0, bstrWide, -1, text_1251, length, NULL, NULL);
  26.         SysFreeString(bstrWide);
  27.         text_to_encode.clear();
  28.         for (int i = 0; i < strlen(text_1251); i++) {
  29.             text_to_encode += text_1251[i];
  30.         }
  31.         delete[]text_1251;
  32.     }
  33.     else if (encode_mode == encode_mode::WINtoUTF) {
  34.         length = MultiByteToWideChar(1251, 0, text, strlen(text), NULL, NULL);
  35.         bstrWide = SysAllocStringLen(NULL, length);
  36.         MultiByteToWideChar(1251, 0, text, strlen(text), bstrWide, length);
  37.         length = WideCharToMultiByte(CP_UTF8, 0, bstrWide, -1, NULL, 0, NULL, NULL);
  38.         char *text_u8 = new char[length];
  39.         WideCharToMultiByte(CP_UTF8, 0, bstrWide, -1, text_u8, length, NULL, NULL);
  40.         SysFreeString(bstrWide);
  41.         text_to_encode.clear();
  42.         for (int i = 0; i < strlen(text_u8); i++) {
  43.             text_to_encode += text_u8[i];
  44.         }
  45.         delete[]text_u8;
  46.     }
  47.     delete[]text;
  48.     return text_to_encode;
  49. }
  50.  
  51. int main() {
  52.     setlocale(LC_ALL, "ru");
  53.     std::string path, dir;
  54.     std::cout << "Путь до файла: ";
  55.     SetConsoleCP(1251);
  56.     std::getline(std::cin, path);
  57.     SetConsoleCP(866);
  58.     if (path[0] == '\"' && path[path.size() - 1] == '\"') {
  59.         path.erase(0, 1);
  60.         path.erase(path.size() - 1);
  61.     }
  62.     else if (path[0] == '\"') {
  63.         path.erase(0, 1);
  64.     }
  65.     if (path[path.size() - 2] != '\\' && path[path.size() - 1] != '\\') {
  66.         dir = path + "\\";
  67.     }
  68.     else {
  69.         dir = path;
  70.         path.erase(path.size() - 1);
  71.     }
  72.     system("cls");
  73.     std::cout << "Расширение файлов: ";
  74.     std::string file_expansion;
  75.     std::cin >> file_expansion;
  76.     if (file_expansion[0] != '.') {
  77.         file_expansion.insert(0, ".");
  78.     }
  79.     system("cls");
  80.     std::cout << "Количество файлов: ";
  81.     int num_files;
  82.     std::cin >> num_files;
  83.     std::fstream write_fs;
  84.     int point_end, delta_point;
  85.     for (int i = 1; i <= num_files; ++i) {
  86.         point_end = dir.size();
  87.         dir += (std::to_string(i) + file_expansion);
  88.         delta_point = dir.size() - point_end;
  89.         write_fs.open(dir, std::fstream::out | std::fstream::app);
  90.         write_fs << encoder("Файл №" + std::to_string(i), encode_mode::WINtoUTF);
  91.         write_fs.close();
  92.         dir.erase(dir.size() - delta_point);
  93.     }
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment