c++ - How to count the different packet number in two text file -
i have 2 text files in.text
, out.text
. read them in unsigned char **
, in each element of array stores data length t=32 bellow code
char *filename = "in.text"; file *stream; int numpackets = 10; int t = 32; // length of each packets unsigned char **packetsin; fopen_s(&stream, filename, "rb"); fseek(stream, 0, seek_set); (int = 0; < numpackets; i++) { fread(packetsin[i], 1, t, stream); } fclose(stream);
in same manner way, can obtain packetsout
above code
filename = "out.text"; file *streamout; numpackets = 10; t = 32; // length of each packets unsigned char **packetsout; fopen_s(&streamout, filename, "rb"); fseek(streamout, 0, seek_set); (int = 0; < numpackets; i++) { fread(packetsout[i], 1, t, streamout); } fclose(streamout);
i want count how many different packet in packetsin
, packetout
(each of them has 10 packets, compare first packet in packetsin
, first packet in packetsout
. if different, count increases 1). me solve it
this tried
int count = 0; (int = 0; < numpackets; i++) { if (packetsin[i] != packetsout[i]) count++; }
packets arrays of bytes, must allocate memory these arrays, either automatic storage, local function or heap malloc
. furthermore, cannot compare arrays ==
or !=
, need use function performs byte byte comparison. memcmp
declared in <string.h>
, returns non 0 value if arrays differ.
here corrected version:
#include <string.h> int compare_packets(void) { file *stream; int numpackets = 10; int t = 32; // length of each packet unsigned char packetsin[numpackets][t]; unsigned char packetsout[numpackets][t]; fopen_s(&stream, "in.text", "rb"); (int = 0; < numpackets; i++) { fread(packetsin[i], 1, t, stream); } fclose(stream); fopen_s(&stream, "out.text", "rb"); (int = 0; < numpackets; i++) { fread(packetsout[i], 1, t, stream); } fclose(stream); int count = 0; (int = 0; < numpackets; i++) { if (memcmp(packetsin[i], packetsout[i], t) count++; } return count; }
Comments
Post a Comment