Can anyone tell me how to compare two files in C byte by byte
Printable View
Can anyone tell me how to compare two files in C byte by byte
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.
is there a function called bcompare,if there is,how do i use it.Is it easier than fopen and fgetc?
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
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...
C will let you compare structs.Code:if (thisaddr != thataddr)
storethisaddr(thisaddr);
> 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.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’)
> 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.
> 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.