Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <fstream>
- #include <Windows.h>
- using namespace std;
- #define O_F_NAME "output.txt"
- int main()
- {
- char f_name[80], need;
- cout << "Enter the file: ";
- cin.get(f_name, 80);
- ifstream in(f_name, ios::in);
- if (!in) {
- cout << "Error in opening file.\n\n";
- system("pause");
- exit(1);
- }
- cout << "\nEnter symbol for find: ";
- cin >> need;
- ofstream out(O_F_NAME, ios::out);
- if (!out) {
- cout << "Error in creating output file.\n\n";
- system("pause");
- exit(2);
- }
- out << "Result of searching for \"" << need << "\":\n\n";
- // main cycle
- int i = 0, eofv = iostream::traits_type::eof();
- int prev = eofv, cur = eofv;
- bool needExit = false;
- while (!needExit) {
- int prevprev = prev;
- prev = cur;
- cur = in.get();
- needExit = (cur == eofv);
- if (prev != need) continue;
- out << ++i << ") '";
- if (prevprev != eofv) out << (char)prevprev;
- out << (char)prev;
- if (cur != eofv) out << (char)cur;
- out << "'\n";
- }
- cout << "\nSuccess. Found " << i << " matches.\n\n"
- << "Results of searching \"" << need << "\" in \"" << f_name
- << "\" placed in \"" << O_F_NAME << "\".\n\n";
- out.close();
- in.close();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement