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.
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?