I want binary compare

This is a discussion on I want binary compare within the Windows Programming forums, part of the Platform Specific Boards category; I want to compare two equal sized files byte to byte. In fact I did this with Delphi(via TMemoryStream) but ...

  1. #1
    Unregistered
    Guest

    I want binary compare

    I want to compare two equal sized files byte to byte. In fact I did this with Delphi(via TMemoryStream) but wanted a more quick compare. So tried via BC++Compiler(commandline - as "C" & "API"). But I am very confused. Delphi did approx. 30 times faster. I used, CreateFile, SetFilePointer and ReadFile API func. for this. And one more tried to add VirtualAlloc even but thats slower In fact here is my code(more clear.) What is wrong code or compiler. Finishing time is too different.
    (note: some controller codes removed. mem, file etc.)
    /////////////////////
    void Compare(HWND _hwnd)
    {
    char *szFile1="file1.dat";
    char *szFile2="file2.dat";
    HANDLE hFile1,hFile2;
    DWORD size1,size2;
    DWORD bayt1 = 0;
    DWORD bayt2 = 0;
    DWORD numofbyts = 0;


    hFile1=CreateFile(szFile1,GENERIC_READ + GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    hFile2=CreateFile(szFile2,GENERIC_READ + GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    size1=GetFileSize(hFile1,0);
    size2=GetFileSize(hFile2,0);

    if(size1!=size2)
    {
    MessageBox (NULL, "bla" , "Err", 0 + MB_ICONHAND);
    return;
    }

    for (unsigned int i=0;i<size1;i++)
    {
    SetFilePointer(hFile1, i, NULL, FILE_BEGIN);
    ReadFile(hFile1,&bayt1,1,&numofbyts,NULL);
    SetFilePointer(hFile2, i, NULL, FILE_BEGIN);
    ReadFile(hFile2,&bayt2,1,&numofbyts,NULL);
    if(bayt1!=bayt2) counter++;
    }

    CloseHandle(hFile1);
    CloseHandle(hFile2);
    MessageBox (_hwnd, "Files Closed" , "Result", 0 + MB_ICONASTERISK);
    }

    /////////////////////

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    It's simple, your algorithm sucks!

    1) You are reading a byte at a time for gods sake! File I/O is really slow, load a block into memory and compare it.

    2) You are resetting the file pointer every time you loop when you really dont need to.

    3) use CODE tags when you post.

    this is the kind of thing you want to do

    Code:
    BYTE buffer1 = new BYTE[size1];
    BYTE buffer2 = new BYTE[size1];
    
    ReadFile(hFile1, buffer1, size1, &numofbyts, NULL);
    ReadFile(hFile2, buffer2, size1, &numofbyts, NULL);
    
    BOOL same = memcmp(buffer1, buffer2, size1) == 0;
    
    delete buffer1;
    delete buffe2;
    hope this helps
    U.

    PS. i didn't compile the above code, it's intended to give you an idea.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Unregistered
    Guest
    I know the code sucks. Because I used what I found in winapi help files. I am learner. And learned this time. So... anyway thaks.
    (btw please don't use such words.)

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    hey, i wasn't having a go at ya.. i was just being frank. I'm not one to beat about the bush, you wanted help, you got it. Don't take it so personally.

    if you learned from it, then mission accomplished! you wanted to learn, and i wanted to teach.

    no hard feelings eh!?

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Unregistered
    Guest
    OK. I suppose it was my bad day. And I am not accustomed to such things. I forgot. Btw, I wanted to do this byte to byte, so it was different. But I solved this via mapping both files to mem. So, now very very quick and I can store all address, orijinal and different bytes.

    Cya,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Large Binary Compare
    By Robert Austin in forum C++ Programming
    Replies: 1
    Last Post: 02-07-2003, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21