Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Windows.h>
- #include <string>
- #include <fstream>
- enum encode_mode {
- UTFtoWIN,
- WINtoUTF
- };
- std::string encoder(std::string text_to_encode, int encode_mode) {
- BSTR bstrWide;
- int length;
- char *text = new char[text_to_encode.size() + 1];
- for (int i = 0; i < text_to_encode.size(); i++) {
- text[i] = text_to_encode[i];
- }
- text[text_to_encode.size()] = '\0';
- if (encode_mode == encode_mode::UTFtoWIN) {
- length = MultiByteToWideChar(CP_UTF8, 0, text, strlen(text), NULL, NULL);
- bstrWide = SysAllocStringLen(NULL, length);
- MultiByteToWideChar(CP_UTF8, 0, text, strlen(text), bstrWide, length);
- length = WideCharToMultiByte(1251, 0, bstrWide, -1, NULL, 0, NULL, NULL);
- char *text_1251 = new char[length];
- WideCharToMultiByte(1251, 0, bstrWide, -1, text_1251, length, NULL, NULL);
- SysFreeString(bstrWide);
- text_to_encode.clear();
- for (int i = 0; i < strlen(text_1251); i++) {
- text_to_encode += text_1251[i];
- }
- delete[]text_1251;
- }
- else if (encode_mode == encode_mode::WINtoUTF) {
- length = MultiByteToWideChar(1251, 0, text, strlen(text), NULL, NULL);
- bstrWide = SysAllocStringLen(NULL, length);
- MultiByteToWideChar(1251, 0, text, strlen(text), bstrWide, length);
- length = WideCharToMultiByte(CP_UTF8, 0, bstrWide, -1, NULL, 0, NULL, NULL);
- char *text_u8 = new char[length];
- WideCharToMultiByte(CP_UTF8, 0, bstrWide, -1, text_u8, length, NULL, NULL);
- SysFreeString(bstrWide);
- text_to_encode.clear();
- for (int i = 0; i < strlen(text_u8); i++) {
- text_to_encode += text_u8[i];
- }
- delete[]text_u8;
- }
- delete[]text;
- return text_to_encode;
- }
- int main() {
- setlocale(LC_ALL, "ru");
- std::string path, dir;
- std::cout << "Путь до файла: ";
- SetConsoleCP(1251);
- std::getline(std::cin, path);
- SetConsoleCP(866);
- if (path[0] == '\"' && path[path.size() - 1] == '\"') {
- path.erase(0, 1);
- path.erase(path.size() - 1);
- }
- else if (path[0] == '\"') {
- path.erase(0, 1);
- }
- if (path[path.size() - 2] != '\\' && path[path.size() - 1] != '\\') {
- dir = path + "\\";
- }
- else {
- dir = path;
- path.erase(path.size() - 1);
- }
- system("cls");
- std::cout << "Расширение файлов: ";
- std::string file_expansion;
- std::cin >> file_expansion;
- if (file_expansion[0] != '.') {
- file_expansion.insert(0, ".");
- }
- system("cls");
- std::cout << "Количество файлов: ";
- int num_files;
- std::cin >> num_files;
- std::fstream write_fs;
- int point_end, delta_point;
- for (int i = 1; i <= num_files; ++i) {
- point_end = dir.size();
- dir += (std::to_string(i) + file_expansion);
- delta_point = dir.size() - point_end;
- write_fs.open(dir, std::fstream::out | std::fstream::app);
- write_fs << encoder("Файл №" + std::to_string(i), encode_mode::WINtoUTF);
- write_fs.close();
- dir.erase(dir.size() - delta_point);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment