Results 1 to 7 of 7

Thread: Find and replace a text string in a binary file

Threaded View

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Resolved 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?
    Last edited by wey97; Nov 10th, 2004 at 09:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width