Thread: binary file copy has extra character

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    15

    binary file copy has extra character

    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(&currentChar, 1, 1, in);
    	inCnt++;
    
    	fwrite( &currentChar, 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

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You mean the output file ends up one character longer than the input file, right?
    I don't think fclose() adds and EOF. I would guess fopen() does that when it creates the file.
    In any case, when fread() reads the EOF then the do-while loop doesn't end. Instead you write the currentChar in the outpute file (out). Whatever the currentChar is you write an additional character. A quick fix might be:
    Code:
      while (1) {
    	fread(&currentChar, 1, 1, in);
    	if (feof(in) break;
            inCnt++;
    	fwrite( &currentChar, 1, 1, out );
    	outCnt++;
      }

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I think better yet
    Code:
    while (fread(&currentChar, 1, 1, in) == 1)
    {
        inCnt++;
        fwrite( &currentChar, 1, 1, out );
        outCnt++;
    }
    or

    Code:
    unsigned char buf[BUF_SIZ];
    size_t len;
    while((len = fread(buf, 1, sizeof buf, in) ) > 0)
    {
        inCnt += len;
        len = fwrite( buf, 1, len, out );
        outCnt += len;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. Large Binary File Copy
    By Robert Austin in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2003, 07:57 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM