I need to modify strings in a binary file (c-tree database file on Windows). I thought it would be easy to write a stream editor to read the file a byte at a time, look for and replace the string, and write the stream to a new file.
I've hit a couple of snags. The first is the file size. To simplify the code, I modified to fread and fwrite each byte. The file ends up one character (0xFF) longer. Here's the simplified code:
Code:#include <stdio.h> #include <windows.h> char currentChar; char holdStr[25]; FILE *in; FILE *out; char newString[20]="\\\\SERVER\\"; char oldString[20]="\\\\server1\\"; char space[10]=" "; int createFile() { int result; int inCnt=0, outCnt=0; int matchCntr=0; int charCnt; char char_buffer[500]; char char_dir[500]; char shortpath[500]; WIN32_FIND_DATA FindFileData; HANDLE hFind; do { fread(¤tChar, 1, 1, in); inCnt++; fwrite( ¤tChar, 1, 1, out ); outCnt++; } while (!feof(in)); return(0); } int main(int argc, char *argv[]) { in = fopen(".\\images.dat", "rb"); if ( in == NULL) { printf("could not open .\\images.dat\n"); exit(1); } out = fopen(".\\images_new.dat", "wb"); if ( out == NULL) { printf("could not open .\\images_new.dat\n"); exit(1); } createFile(); fclose(in); fclose(out); exit(0); }
Can someone help me understand what is happening? Does fclose write an EOF?
Thanks,
Leon