Advertisement
heroys6

whereIt

Sep 7th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <fstream>
  4. #include <Windows.h>
  5. using namespace std;
  6.  
  7. #define O_F_NAME "output.txt"
  8.  
  9. int main()
  10. {
  11.     char f_name[80], need;
  12.  
  13.     cout << "Enter the file: ";
  14.     cin.get(f_name, 80);
  15.  
  16.     ifstream in(f_name, ios::in);
  17.     if (!in) {
  18.         cout << "Error in opening file.\n\n";
  19.         system("pause");
  20.         exit(1);
  21.     }
  22.  
  23.     cout << "\nEnter symbol for find: ";
  24.     cin >> need;
  25.  
  26.     ofstream out(O_F_NAME, ios::out);
  27.     if (!out) {
  28.         cout << "Error in creating output file.\n\n";
  29.         system("pause");
  30.         exit(2);
  31.     }
  32.  
  33.     out << "Result of searching for \"" << need << "\":\n\n";
  34.  
  35. // main cycle
  36.  
  37.     int i = 0, eofv = iostream::traits_type::eof();
  38.     int prev = eofv, cur = eofv;
  39.     bool needExit = false;
  40.  
  41.     while (!needExit) {
  42.         int prevprev = prev;
  43.         prev = cur;
  44.         cur = in.get();
  45.  
  46.         needExit = (cur == eofv);
  47.  
  48.         if (prev != need) continue;
  49.  
  50.         out << ++i << ") '";
  51.         if (prevprev != eofv) out << (char)prevprev;
  52.         out << (char)prev;
  53.         if (cur != eofv) out << (char)cur;
  54.         out << "'\n";
  55.     }
  56.    
  57.     cout << "\nSuccess. Found " << i << " matches.\n\n"
  58.         << "Results of searching \"" << need << "\" in \"" << f_name
  59.         << "\" placed in \"" << O_F_NAME << "\".\n\n";
  60.  
  61.     out.close();
  62.     in.close();
  63.     system("pause");
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement