Replace character in txt file Problem.

Hi I have a problem to replace some specifics characters in a text file. First, I read the file and wanna replace some specific characters. Please see my code and give me advices. I use FILE to creat a pointer. But I do not know how to find the specific character that I want to replace.
For example, I wanna replace all the '7' to space in the file below.
1
2
3
4
5
6
{"2up72PW7F6`7m$67H?j7`67"jPu?je6WH7mPW6e7P6F7p2F$

dC675j,6eP"6P?7Fupp7`672`p67?j7"jPu?je7?C67(2pp$k76"2up$k7?6%?$72PW
F6`$u?67,u$u?$7j_76,6eQjP67uP7?C67+&7mPW6e7P6F7p65u$p2?ujP7$6?7?j7`6
2PPjmP(6W7$jjPs77MP?6eP6?7_ue"$7Fupp7`67e6;mue6W7?j75u,67uP?6ppu56P(6
256P(Q7^DEx72((6$$7?j7(j""mPu(2?ujP$7jP7W6"2PWk7uP7e62p7?u"6s

this is my function that for replacing characters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Parser::decryption()
{
	int charReplace1, charReplace2;
	FILE *fp1;
	const char *inname = "text1.txt";
	cout<<"Input the character(int) you want to replace: \n";
	cin>>charReplace1;
	cout<<"Input the character(int) you want to replace to : \n";
	cin>>charReplace2;
	p1 = fopen(inname, "rb+");
	if(!fp1)
	{
		cout<<"Cannot open file" <<endl;
	}
	int inpch;
	while((inpch = fgetc(fp1))!=EOF)
	{
		fseek(fp1, -1, SEEK_CUR);
		fputs("e", fp1);//Here is just test code, I don't know how to  read it.
	}
	fclose(fp1);	
}
upload it into a string and then move across each char with a for loop replacing each 7 with whatever you want. then put the string into the file again.
EDIT: i was reported for this...
Last edited on
Any code help? I try a lot of methods, but does not work.
1
2
3
4
5
filename >> stringname; 
// use a better way to get string from file
for(int i = 0; i < stringname.length(); i++){ 
if(stringname[i] == '7') stringname[i] == ' ';
} 
Last edited on
It still does not work as I thought..
What ui uiho posted will get the string and replace all instances of "7" with a space (" "). He did not post anything for returning the string to the file, so you have to write that yourself.

I'm assuming that's the error, because you didn't post what your exact error was
Topic archived. No new replies allowed.