TGC Codebase Backup



Binary File Comparison by heartbone

12th Mar 2007 12:13
Summary

Allows you to compare two files for differences at the byte level. The program displays the first 10 differences in the files.



Description

Allows you to compare two files for differences at the byte level.
You type in the two file names to compare.
The program displays the first 10 differences in the files.
If the file sizes are different, the compare is performed for the number of bytes in the smaller file.
Tested in DarkBASIC v1.13 and DarkBASIC Professional v4.1.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Print
Print "FILE COMPARE - Reports the first 10 differences"
GETFIR_:
Print
Input "Enter first filename to compare: ",F1$

If File Exist(F1$)
   FS1= File Size(F1$)
   If FS1 > 0
GETSEC_:
      Print
      Input "Enter second filename to compare: ",F2$
      If File Exist(F2$)
         FS2= File Size(F2$)
         If FS2 > 0
            NDIFF= 0
            SCANSIZE= FS1
            If FS1 <> FS2
               If FS2 < FS1 Then SCANSIZE= FS2
               Print
               Print "The files are different sizes."
               Print "Only the first "+Str$(SCANSIZE)+" bytes will be compared."
            Endif
            Open To Read 1, F1$
            Open To Read 2, F2$
            For IND= 1 To SCANSIZE
               Read Byte 1,FBYT1
               Read Byte 2,FBYT2
               If FBYT1 <> FBYT2
                  Inc NDIFF
                  Print "Byte #"+Str$(IND)+" File1 ="+Str$(FBYT1)+" File2 ="+Str$(FBYT2)
                  If NDIFF = 10 Then Goto FINI_
               Endif
            Next IND
FINI_:
            Close File 1
            Close File 2
            If NDIFF = 0
               Print
               Print "Files Are Identical"
            Endif
            Print
            Print "Press any key."
            Wait Key
            End
         Else
            Print
            Print F2$+" is empty."
            Goto GETSEC_
         Endif
      Else
         Print
         Print "That File Does Not Exist!"
         Goto GETSEC_
      Endif
   Else
      Print
      Print F1$+" is empty."
      Goto GETFIR_
   Endif
Else
   Print
   Print "That File Does Not Exist!"
   Goto GETFIR_
Endif