Outputting characters from one file to another

Hello,

I am writing a code that searches through a text file for the word 'ricin.' I need to create he cod so that if this word is found, the code will output 200 characters before and after this word into a next text file. I think I have everything needed in order to find the word, but I am stuck on how to output to the new file. I used seekg to put the cursor back 200 characters and was going to use a for loop in order to output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream> 
#include <fstream>
#include <string>

using namespace std;

int main() 
{
    string infile, outfile;
    char character;
    ifstream text;
    ofstream textout;
    bool text_state;
    int count=0;
    
        cout << "What is the name of the text file to be examined? ";
        cin >> infile;

    
    text.open(infile.c_str());
    if(text.fail())
    {
                   cerr << "Error: File not found.";
                   exit(1);
    }
    
    textout.open("outfile.txt");
    
    while (text.get(character))
    {
                                   if ((character == 'r') || (character == 'R'))
                                   {
                                       if ((character == 'i') || (character == 'I'))
                                       {
                                                 if ((character == 'c') || (character == 'C'))
                                                 {
                                                               if ((character == 'i') || (character == 'I'))
                                                               {
                                                                             if ((character == 'n') || (character == 'N'))
                                                                             {
                                                                                 text.seekg(-200,std::ios::cur);
                                                                                 for (count=0; count=400; count++)
                                                                                 {
                                                                                     textout << text;
                                                                                     }
                                                                                 
                                                                                 }          
                                                                             else
                                                                              {
                                                                                    text.get(character);
                                                                              }
                                                                              }             
                                                               else
                                                               {
                                                                           text.get(character);
                                                               }
                                                               }
                                                  else
                                                  {
                                                            text.get(character);
                                                  }
                                                  }
                                    else
                                   {
                                       text.get(character);
                                   }   
                                   }       
                                   else
                                   {
                                       text.get(character);
                                   }
                                   }
                                   
                      text.close();
                      textout.close();
                      system("pause");
                      return 0;
    

} 
Shouldn't your loop (42-45) both read characters and write them?
I want the code to read the characters from one file and then write them to another file.
I've tried inserting a cout << "Is this working?";
Before line 44 to see if the program is actually working, and then inserted a file that I know had the word ricin in it, and my program did not output anything. I am now wondering if I have the correct command for it to scan the text file.
Oh, yes, that too.
Think about the result of the condition on line 33. It will be tested only if line 31's condition i true. In other words you will test whether "character" is 'i' only if you are certain that the same variable has value 'r'.

How about this:
* read a line at a time
* if the line contains the keyword, it should be easy to continue to read input until the trailing text is in memory
* do not discard the line, but append it into in-memory buffer
* if no keyword has been found, discard the oldest part of the buffer
* when the keyword is found, the buffer has the leading text


Other considerations:
* what if there are multiple occurrences of keyword?
* does the keyword have to be a whole word, or can it be a subset of a word?
Topic archived. No new replies allowed.