Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- using namespace std;
- bool writer(const wstring path, const vector<wstring>& content) {
- wofstream stream(path);
- stream.imbue(locale(".UTF-8"));
- if (stream.is_open() && !stream.bad()) {
- for (const auto& line : content) stream << line << L'\n';
- stream.close();
- return true;
- }
- return false;
- }
- vector<wstring> reader(const wstring path) {
- vector<wstring> content;
- wifstream stream(path);
- stream.imbue(locale(".UTF-8"));
- if (stream.is_open() && !stream.bad()) {
- wstring line;
- while (getline(stream, line)) content.emplace_back(line);
- stream.close();
- }
- return content;
- }
- void test(wstring&& str) {
- vector<wstring> content;
- content.emplace_back(move(str));
- wstring line;
- wcout << L"Введите пару строк на русском языке:\n";
- for (auto i = 0U; i < 2; ++i) {
- getline(wcin, line);
- content.emplace_back(line);
- }
- wcout.put(L'\n');
- wstring path = LR"(D:\content.txt)";
- if (writer(path, content)) {
- auto result = reader(path);
- if (result.empty()) wcout << L"Информация не найдена!\n";
- else for (const auto& line : result) wcout << line << L'\n';
- } else wcout << L"Ошибка записи в файл.\n";
- }
- int main() {
- wcout.imbue(locale(".OCP"));
- wcin.imbue(locale(".OCP"));
- const wchar_t* message = L"Этот текст на русском языке:";
- test(wstring(message));
- wcout << LR"(Ура! Работает!
- Откройте в Notepad++ файл D:\content.txt)";
- wcout.put(L'\n');
- system("pause");
- }
Add Comment
Please, Sign In to add comment