Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <vector>
- #include <algorithm>
- #include <fstream>
- #include <windows.h>
- #include <codecvt>
- #undef max
- HANDLE hStdOut;
- void ReadFile(std::vector<std::wstring>& text);
- void Search(std::vector<std::wstring>& text);
- void SearchInString(std::wstring& str, std::wstring tgt);
- int main()
- {
- hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); // хэндл для изменения цвета текста
- std::wcin.imbue(std::locale("rus_rus.866")); // рус яз
- std::wcout.imbue(std::locale("rus_rus.866"));
- std::vector<std::wstring> text; // массив строк
- std::wstring buffer;
- std::wstring path;
- for (;;)
- {
- std::wcout << L"Введите путь к файлу с текстом:" << std::endl;
- std::getline(std::wcin, path);
- if (_waccess_s(path.c_str(), 4)) // проверка файла
- {
- std::wcout << L"Не удалось получить доступ к файлу" << std::endl;
- continue;
- }
- break;
- }
- 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;
- checkBom.close();
- std::wifstream file(path); // создание файлового потока
- if (isBom)
- 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);
- std::wcout << L"\nПолучившийся текст:" << std::endl;
- for (const auto& line : text) // печатаем
- {
- //line.erase(std::remove_if(line.))
- std::wcout << line << std::endl;
- }
- std::wcout << std::endl;
- if (text.empty())
- {
- std::wcout << L"Файл пуст" << std::endl;
- system("pause");
- return 0;
- }
- int line, menu = 0;
- for (;;)
- {
- std::wcout << L"Выберите действие:" << std::endl;
- std::wcout << L"1) поиск подстроки во всем тексте" << std::endl;
- std::wcout << L"2) поиск подстроки в строке" << std::endl;
- std::wcout << L"3) вставка текста из файла" << std::endl;
- std::wcout << L"4) выход\n" << std::endl;
- if (!(std::wcin >> menu))
- {
- std::wcout << L"Ошибка ввода" << std::endl;
- std::wcin.clear();
- std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
- continue;
- }
- switch (menu)
- {
- case 1:
- Search(text);
- break;
- case 2:
- for (;;)
- {
- std::wcout << L"Номер строки:" << std::endl;
- if (!(std::wcin >> line) || line < 1 || line > text.size())
- {
- std::wcout << L"Ошибка ввода" << std::endl;
- std::wcin.clear();
- std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
- continue;
- }
- break;
- }
- std::wcout << L"Искомая подстрока:" << std::endl;
- std::wcin.clear();
- std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
- std::getline(std::wcin, buffer);
- SearchInString(text[line - 1], buffer);
- break;
- case 3:
- ReadFile(text);
- break;
- case 4:
- system("pause");
- return 0;
- default:
- std::wcout << L"Ошибка! Такого пункта не существует" << std::endl;
- }
- }
- }
- void ReadFile(std::vector<std::wstring>& text)
- {
- std::wstring path;
- for (;;)
- {
- std::wcout << L"Введите путь к файлу с текстом:" << std::endl;
- std::wcin.clear();
- std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
- std::getline(std::wcin, path);
- if (_waccess_s(path.c_str(), 4)) // проверка файла
- {
- std::wcout << L"Не удалось получить доступ к файлу" << std::endl;
- continue;
- }
- break;
- }
- std::wstring buffer;
- 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;
- checkBom.close();
- std::wifstream file(path); // создание файлового потока
- if (isBom)
- 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);
- std::wcout << L"\nПолучившийся текст:" << std::endl;
- for (const auto& line : text) // печатаем
- std::wcout << line << std::endl;
- std::wcout << std::endl;
- }
- void Search(std::vector<std::wstring>& text)
- {
- std::wstring target;
- std::wcout << L"Искомая подстрока:" << std::endl;
- std::wcin.clear();
- std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
- std::getline(std::wcin, target);
- std::wcout << std::endl;
- for (auto& str : text)
- SearchInString(str, target);
- std::wcout << std::endl;
- }
- void SearchInString(std::wstring& str, std::wstring tgt)
- {
- int prevPos = 0; // предыдущая позиция, в которой нашли символ
- for (int pos = 0 - tgt.length();;)
- {
- pos = str.find(tgt, pos + tgt.length()); // ищем строку
- if (pos == std::wstring::npos) // если не нашли
- {
- std::wcout << str.substr(prevPos) << std::endl; // печатаем все, что осталось
- return;
- }
- std::wcout << str.substr(prevPos, pos - prevPos); // печатаем до найденного
- SetConsoleTextAttribute(hStdOut, FOREGROUND_GREEN); // делаем зеленый цвет
- std::wcout << str.substr(pos, tgt.length()); // саму найденную подстроку
- SetConsoleTextAttribute(hStdOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); // возвращаем цвет
- prevPos = pos + tgt.length();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement