Find and replace a text string in a binary file
I have code to read the file and print it out. I need to search for a string and replace it so I figured it would be better to do it in C++.
Code:
unsigned char ch;
int linecounter=0;
FILE *in;
in = fopen("myfile.bin","rb");
if(ferror(in) ){
perror("Error opening input file:");
exit(1);
}
while (!feof(in) ){
if( fread(&ch,sizeof(ch),1,in) ==1){
/* do stuff with the character you just read */
printf("%c",ch);
linecounter++;
if ( !(linecounter%30) )
printf("\n");
}
}
fclose(in);
Can this be done in C++ to get the data in a string?