CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2001
    Posts
    20

    Help Reading Bytes for File Compare

    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 [email protected] if you can help

    Cheers




  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Help Reading Bytes for File Compare

    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




  3. #3
    Join Date
    May 2001
    Posts
    20

    Re: Help Reading Bytes for File Compare

    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

    cheers

    Dom


  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    HelpRight!

    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.
    ...at present time, using mainly Net 4.0, Vs 2010



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

  5. #5
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Help Reading Bytes for File Compare

    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





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

Featured