Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <Windows.h>
- using namespace std;
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- ifstream fin;
- ofstream fout;
- fin.open("file.txt");
- fout.open("file2.txt");
- if (!fin.is_open() || !fout.is_open()) {
- cout << "Ошибка открытия файла" << endl;
- }
- else {
- int count = 0;
- string word, find_word, new_word, buf;
- cout << "Введите слово для поиска: ";
- getline(cin, find_word);
- cout << "Введите слово для замены: ";
- getline(cin, new_word);
- while (!fin.eof()) {
- getline(fin, word);
- for (int i = 0; i < word.size(); i++) {
- if (word[i] == find_word[0]) {
- for (int j = 0; j < min(word.size(), find_word.size()); j++) {
- if (word[i] != find_word[j]) {
- break;
- }
- buf.push_back(word[i]);
- i++;
- }
- if (buf == find_word) {
- fout << new_word;
- count++;
- }
- else {
- fout << buf;
- }
- buf.clear();
- }
- fout << word[i];
- }
- fout << endl;
- }
- cout << "Произведено замен: " << count << endl;
- fin.close();
- fout.close();
- remove("file.txt");
- rename("file2.txt", "file.txt");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement