Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <cctype>
- /* run this program using the console pauser or add your own getch, system("pause") or input loop */
- using namespace std;
- int main() {
- FILE* file1 = fopen("test.in", "r");
- FILE* file2 = fopen("test.out", "w+");
- string str = "";
- string h = "hello";
- string temp_s = "";
- char temp;
- int a;
- int i = 0;
- if(!file1){
- cout << "Can't open file 'test.in'. You should create it!";
- }
- else{
- fseek(file1, 0, SEEK_END);
- long size = ftell(file1);
- rewind(file1);
- long cur_pos = 0;
- string checkWord;
- string buffer;
- string wordToFind = " hello ";
- while (cur_pos != size){
- // cout << cur_pos<< "\t" << size << endl;
- fseek(file1, cur_pos, SEEK_SET);
- str = "";
- temp = '0';
- while(temp != '\n'){
- fscanf(file1, "%c", &temp);
- str = str + temp;
- cur_pos = ftell(file1);
- if(cur_pos == size) break;
- if(str == "\n") str = "";
- //cout << "\t\t" << str;
- }
- if(str != "")
- {
- buffer = str;
- buffer[buffer.length()-1] = ' ';
- buffer = " " + buffer;
- //cout << buffer << endl;
- for(int i = 0; i < buffer.length()-7; i++){
- checkWord = "";
- for(int j = 0; j < wordToFind.length(); j++){
- checkWord = checkWord + buffer[i+j];
- }
- // cout << checkWord;
- if(checkWord == wordToFind){
- for(int a = 0; a < str.length(); a++){
- fprintf(file2, "%c", str[a]);
- }
- }
- }
- }
- }
- }
- fclose(file1);
- fclose(file2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement