Thread: File Comparision byte by byte

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    File Comparision byte by byte

    Can anyone tell me how to compare two files in C byte by byte

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fopen both, fgetc both in a loop. You might also consider using ==. But really you can do this with any function that reads from a file.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    4
    is there a function called bcompare,if there is,how do i use it.Is it easier than fopen and fgetc?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by anusha2489 View Post
    is there a function called bcompare,if there is,how do i use it.Is it easier than fopen and fgetc?
    In the time it took you to type this, you could have just googled 'bcompare' to see if there was in fact a function called 'bcompare', and if so, how you would use it.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    In the time it took you to type this, you could have just googled 'bcompare' to see if there was in fact a function called 'bcompare', and if so, how you would use it.
    Quzah.
    Alternatively, if he has the help documentation for his compiler (most come with it) done the search in about half that time.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    4
    I am actually trying to write a peer-peer program, where I need to compare the peerids that a peer send with the peer ids that I have and add those that I do not have. Peerid is struct with port and ip.
    How do i do this

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anusha2489 View Post
    I am actually trying to write a peer-peer program, where I need to compare the peerids that a peer send with the peer ids that I have and add those that I do not have. Peerid is struct with port and ip.
    How do i do this
    Well, the first thing you need to do is decide which project you're writing... are you comparing files or comparing IP addresses?

    Take my word for this... if you give us wrong information you will get wrong answers.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    4
    I need compare IP addresses and port no.s

    The peer id is IP+Port no.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anusha2489 View Post
    I need compare IP addresses and port no.s

    The peer id is IP+Port no.
    Ok for that you can use the already defined SOCKADDR_IN structure which is filled in by recvfrom() in the sockets library.

    Just store a list of already received structs in an array (sorting is optional depending how many you have). Then compare...
    Code:
    if (thisaddr != thataddr)
      storethisaddr(thisaddr);
    C will let you compare structs.
    Last edited by CommonTater; 05-15-2011 at 06:22 PM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > C will let you compare structs.
    Are you sure?
    Code:
    $ cat bar.c
    struct foo { int bar ;};
    int main ( ) {
      struct foo a, b;
      if ( a == b );
      return 0;
    }
    $ gcc bar.c
    bar.c: In function ‘main’:
    bar.c:4: error: invalid operands to binary == (have ‘struct foo’ and ‘struct foo’)
    If you want to compare a struct, you need to use == (or strcmp, or memcmp) for each member of the structure.


    > I am actually trying to write a peer-peer program,
    If you're still struggling with comparisons, then I would suggest you practice C some more, before trying to write an actual program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > C will let you compare structs.
    Are you sure?
    If you want to compare a struct, you need to use == (or strcmp, or memcmp) for each member of the structure.
    OOPs... I'm thinking some Pascal leaked in there somehow... yes, memcmp() works... my bad.

    (and yes, that still happens sometimes... I'll be working on something and here's this whole chain of a := b; stuff....)

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > yes, memcmp() works... my bad.
    Do you want to try again?

    memcmp() does not work on two structs, because you have no control over what data lies in all the slack/padding bytes between members.

    You have to compare each member, with whatever makes most sense for each member.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > yes, memcmp() works... my bad.
    Do you want to try again?

    memcmp() does not work on two structs, because you have no control over what data lies in all the slack/padding bytes between members.

    You have to compare each member, with whatever makes most sense for each member.
    Ok... would you believe... Chuck Norris with a BB gun?

    Hmmmm... you're right again...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading files byte by byte
    By cpsc in forum C++ Programming
    Replies: 12
    Last Post: 01-07-2011, 03:54 PM
  2. TIFF IFD byte by byte
    By ghostcoder in forum C++ Programming
    Replies: 4
    Last Post: 12-21-2010, 04:33 PM
  3. end of file byte
    By Aisthesis in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 03:28 PM
  4. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM