Click to See Complete Forum and Search --> : Help Reading Bytes for File Compare


Dommy
May 18th, 2001, 09:29 AM
Hullo,

I was wondering if anybody out there could help me with some basic syntax and methods of extracting bytes from a file (eg text file) for comparison with the bytes from a another file, for a file compare I've been asked to do.
Put simply, it has to output something like this:
Comparing files file1.txt and file2.txt
0000000A: 31 33
0000000C: 33 31
0000000F: 34 36
00000011: 36 34
00000014: 37 39
00000016: 39 37
00000023: 4D 6D
00000024: 4E 6E
00000025: 4F 6F

How do I read the bytes from file1 so I can compare them with file2... I've done it for ascii compare, but basically I don't know how to read each byte suitably. There's a command I'm sure, anyone know what it is and how to use it? Pretty simple for anyone that's done it I know, but I can't find help notes anywhere for some strange reason.

Email me at Dommy@optusnet.com.au if you can help

Cheers



:D

cksiow
May 18th, 2001, 09:34 AM
you will just have to read the file into a string variable and compare byte by byte.

refer http://vblib.virtualave.net, ReadFileStr in vbFileIO read a file into string variable.

the using Mid, you can extra each byte from the string using a for loop

HTH

Dommy
May 18th, 2001, 09:54 AM
ha **** that will work ... I was trying to use Get() all day! :) Thanks for the quick response too...

I'll let ya know how it all turns out :D

cheers

Dom

:D

Cimperiali
May 18th, 2001, 10:18 AM
Happy it helped.
Best regards,
Cesare Imperiali

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.

shree
May 18th, 2001, 10:27 AM
You can do it using the Get Statement. Here is an example code


'This function gives you the file both as a byte array and a string
'Using the byte array to process the bytes individually is faster than using mid$
'to extract each character from a string.
'
private Function OpenAFile(Filename as string) as Long
Dim FileAsByte() as Byte
Dim FileAsStr as string
Dim FileHandle as Long
FileHandle = FreeFile
Open Filename for binary as FileHandle
ReDim FileAsByte(LOF(FileHandle))
get FileHandle, , FileAsByte
FileAsStr = StrConv(FileAsByte(), vbUnicode)

' Do your stuff here

Close FileHandle
TransmitAFile = len(FileAsStr)
FileAsStr = ""
Erase FileAsByte
End Function