I have an assignment for class where we have to create a censoring program. We need to replace target words in a file with '*' characters. EG, I need to turn this:
I wish it would stop raining. |
into this:
I wi** ** would stop raining. |
I managed to do most of it, but ran into a problem that I don't understand.
The way I did this was to create a 4 letter string called buffer and feed it one (alphanumeric) character at a time from the file. Then I compare buffer with the target word and replace the letters with '*' characters.
However, after I replace the letters in the file, I can't get input from the file anymore.
I think this is the function with the problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
//erases first character in buffer and adds a new one to the end
//returns true/false depending on whether or not EOF found
bool Censor::MoveBuffer()
{
//std::cout << "Before get. " << inout.tellg() << std::endl;
char c = inout.get();
if (!inout)
{
std::cout << "Problem with inout after get. " << inout.tellg() << std::endl;
}
while (inout && !isalnum(c))
{
c = inout.get();
}
//...
}
|
After changing
I wish it
to
I wi** **
, there is a problem with using
char c = inout.get()
which causes
inout.tellg()
to return -1. So the function returns as false and the program does not process the rest of the file.
However, if I include the line I commented out where I use
inout.tellg()
before
inout.get()
, without changing anything else in the code, the program works fine. The whole file is processed. I don't understand what's going on.