Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <fstream>
- #include <codecvt>
- void ReadFile(std::vector<std::wstring> & text);
- bool isBom(std::wstring path);
- int main()
- {
- std::wcin.imbue(std::locale("rus_rus.866"));
- std::wcout.imbue(std::locale("rus_rus.866"));
- std::vector<std::wstring> text;
- std::wstring buffer;
- const auto& facet = std::use_facet<std::ctype<wchar_t>>(std::locale());
- for (;;)
- {
- static int action = -1;
- std::wcout << L"1) Ввести новую строку и добавить ее в конец текста\n"
- << L"2) Заменить все первые строчные буквы слов прописными\n"
- << L"3) Вставить строки из файла\n"
- << L"4) Выйти\n\n"
- << L"Вы выбираете действие: ";
- if (!(std::wcin >> action))
- {
- std::wcin.clear();
- std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
- std::wcout << L"Ошибка" << std::endl;
- continue;
- }
- switch (action)
- {
- case 1:
- std::wcout << L"\nВведите строку:" << std::endl;
- std::wcin.clear();
- std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
- std::getline(std::wcin, buffer);
- text.push_back(buffer);
- break;
- case 2:
- std::for_each(text.begin(), text.end(), [&facet](std::wstring& line)
- {
- wchar_t last = ' ';
- std::for_each(line.begin(), line.end(), [&last, &facet](wchar_t& c) {
- if (iswalpha(c) && (last == ' ') && c != ' ')
- c = facet.toupper(c);
- last = c;
- });
- });
- break;
- case 3:
- ReadFile(text);
- break;
- case 4:
- system("pause");
- return 0;
- default:
- std::wcout << L"Нет такого действия" << std::endl;
- }
- std::wcout << L"\nТекст:" << std::endl;
- std::for_each(text.begin(), text.end(), [](const std::wstring& line)
- {
- std::wcout << line << std::endl;
- });
- std::wcout << std::endl;
- }
- }
- void ReadFile(std::vector<std::wstring> & text)
- {
- std::wstring buffer;
- for (;;)
- {
- std::wcout << L"Введите путь к файлу:" << std::endl;
- std::wcin.clear();
- std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
- std::getline(std::wcin, buffer);
- if (_waccess_s(buffer.c_str(), 4))
- {
- std::wcout << L"Не удалось прочитать файл" << std::endl;
- continue;
- }
- break;
- }
- std::wifstream file(buffer);
- if (isBom(buffer))
- file.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t, 0x10ffff, std::consume_header>));
- else
- file.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
- while (std::getline(file, buffer))
- text.push_back(buffer);
- }
- bool isBom(std::wstring path)
- {
- bool isBom = false;
- std::wifstream checkBom(path);
- wchar_t bom[3];
- checkBom.read(bom, 3);
- if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)
- isBom = true;
- return isBom;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement