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.
void Parser::decryption()
{
int charReplace1, charReplace2;
FILE *fp1;
constchar *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...
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] == ' ';
}
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