input/output question

Mar 25, 2013 at 9:59pm
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.
Last edited on Mar 25, 2013 at 10:00pm
Mar 25, 2013 at 10:38pm
If you can use ready libraries, simply use
http://www.cplusplus.com/reference/string/string/find/
to get position of word to censor, and then
http://www.cplusplus.com/reference/string/string/replace/
to replace word at that position
Mar 26, 2013 at 4:08am
Yes, I did use those functions. The problem is that for some reason the file input functions are returning EOF when it's not true. But for some reason, when I use tellg() before using get() then there is no problem. I just want to know why that is.
Mar 26, 2013 at 10:03am
Maybe you did put '\0' somewhere? how do you replace?
It would be the best if you were to paste your source code here for us to analyze.

Also i had fun creating my own version:
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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;
#define IN_FILENAME "asd.txt"
#define OUT_FILENAME "asd2.txt"
#define MAX 3

void censor(string *input, string *word);

int main ()
{
	string	inputstr;
	string	censorwords[MAX] = {"asd1", "ldsa2", "word"};

	ifstream in_file(IN_FILENAME);
	std::getline( in_file, inputstr, '\0' );
	in_file.close();

	cout << "Before:" << endl << inputstr << endl;

	for (int i=0; i<MAX; i++)
		censor(&inputstr, &censorwords[i]);

	cout << "After:" << endl << inputstr << endl;
		
	ofstream out_file(OUT_FILENAME);
	out_file.write(inputstr.c_str(), inputstr.length());
	out_file.close();

	system("pause");
    return 0;
}

void censor(string *input, string *word)
{
	unsigned pos;
	unsigned start_pos=0;

	while(1)
	{
		pos = input->find(*word, start_pos);
		if (pos == std::string::npos)
			break;
		else
		{
			cout << "found \"" << *word << "\" at: " << pos << endl;
			input->replace(pos, word->length(), word->length(), '*');
			start_pos = pos + word->length();
		}
	}
}

Maybe you can use it for different approach.
Topic archived. No new replies allowed.