Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cctype>
  5. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  6. using namespace std;
  7.  
  8. int main() {
  9.     FILE* file1 = fopen("test.in", "r");
  10.     FILE* file2 = fopen("test.out", "w+");
  11.     string str = "";
  12.     string h = "hello";
  13.     string temp_s = "";
  14.     char temp;
  15.     int a;
  16.     int i = 0;
  17.    
  18.     if(!file1){
  19.         cout << "Can't open file 'test.in'. You should create it!";
  20.     }
  21.     else{
  22.         fseek(file1, 0, SEEK_END);
  23.         long size = ftell(file1);
  24.         rewind(file1);
  25.         long cur_pos = 0;
  26.         string checkWord;
  27.         string buffer;
  28.         string wordToFind = " hello ";
  29.    
  30.         while (cur_pos != size){
  31.         //  cout << cur_pos<< "\t" << size << endl;
  32.             fseek(file1, cur_pos, SEEK_SET);
  33.             str = "";
  34.             temp = '0';
  35.             while(temp != '\n'){
  36.                 fscanf(file1, "%c", &temp);
  37.                 str = str + temp;
  38.                 cur_pos = ftell(file1);
  39.                 if(cur_pos == size) break;
  40.                 if(str == "\n") str = "";
  41.                 //cout << "\t\t" << str;
  42.             }
  43.             if(str != "")
  44.             {
  45.  
  46.                 buffer = str;
  47.                 buffer[buffer.length()-1] = ' ';
  48.                 buffer = " " + buffer;
  49.                 //cout << buffer << endl;
  50.            
  51.                 for(int i = 0; i < buffer.length()-7; i++){
  52.                     checkWord = "";
  53.                     for(int j = 0; j < wordToFind.length(); j++){
  54.                         checkWord = checkWord + buffer[i+j];
  55.                     }
  56.                 //  cout << checkWord;
  57.                     if(checkWord == wordToFind){
  58.                         for(int a = 0; a < str.length(); a++){
  59.                             fprintf(file2, "%c", str[a]);
  60.                         }
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.     }  
  66.     fclose(file1);
  67.     fclose(file2);
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement