C++ 如何计算两个文本文件中的不同数据包编号
我有两个文本文件C++ 如何计算两个文本文件中的不同数据包编号,c++,c,C++,C,我有两个文本文件in.text和out.text。我将把它们读入unsigned char**,其中数组的每个元素存储一个长度为T=32的数据,作为下面的代码 char *filename = "in.text"; FILE *stream; int numPackets = 10; int T = 32; // Length of each packets unsigned char **PacketsIn; fopen_s(&stream, filename, "rb"); f
in.text
和out.text
。我将把它们读入unsigned char**
,其中数组的每个元素存储一个长度为T=32的数据,作为下面的代码
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);
for (int i = 0; i < numPackets; i++) {
fread(PacketsIn[i], 1, T, stream);
}
fclose(stream);
我想计算一下PacketsIn
和PacketOut
中有多少不同的数据包(每个数据包有10个数据包,我们将比较PacketsIn
中的第一个数据包和PacketsOut
中的第一个数据包。如果它们不同,则计数增加1)。你能帮我解决这个问题吗
这就是我试过的
int count = 0;
for (int i = 0; i < numPackets; i++) {
if (PacketsIn[i] != PacketsOut[i])
count++;
}
int count=0;
对于(inti=0;i
当memcmp返回非零值时,这表示两个给定字符串之间的差异:
#include <string.h>
if (memcmp(PacketsIn[i],PacketsOut[i],32)!=0)
count++
#包括
if(memcmp(PacketsIn[i],PacketsOut[i],32)!=0)
计数++
数据包是字节数组,您必须为这些数组分配内存,作为自动存储、函数本地存储或使用malloc
从堆中分配。此外,您不能将数组与==
或进行比较=代码>,则需要使用执行逐字节比较的函数<在
中声明的code>memcmp
执行此操作,如果数组不同,则返回非0值
以下是更正的版本:
#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");
for (int i = 0; i < numPackets; i++) {
fread(PacketsIn[i], 1, T, stream);
}
fclose(stream);
fopen_s(&stream, "out.text", "rb");
for (int i = 0; i < numPackets; i++) {
fread(PacketsOut[i], 1, T, stream);
}
fclose(stream);
int count = 0;
for (int i = 0; i < numPackets; i++) {
if (memcmp(PacketsIn[i], PacketsOut[i], T)
count++;
}
return count;
}
#包括
int比较_数据包(无效){
文件*流;
int numPackets=10;
int T=32;//每个数据包的长度
[numPackets][T]中的未签名字符包;
未签名字符包sout[numPackets][T];
fopen_s(&stream,“in.text”,“rb”);
对于(inti=0;i
哪种语言?C
或C++
?我在visual studio中同时使用这两种语言。PacketsIn[I]!=PacketsOut[I]
这只比较两个指针,而不是它们指向的内容。您需要stdcmp()
或memcmp()重要的是要知道C或C++:他用C++容器回答+算法将与基于MCMCP的普通CIF NUBPACK基本上不同,T都是const,你可以声明未签名的CHARCARTETXXX[NUBPAST] [T];如果它是动态的,我建议矢量代替。
#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");
for (int i = 0; i < numPackets; i++) {
fread(PacketsIn[i], 1, T, stream);
}
fclose(stream);
fopen_s(&stream, "out.text", "rb");
for (int i = 0; i < numPackets; i++) {
fread(PacketsOut[i], 1, T, stream);
}
fclose(stream);
int count = 0;
for (int i = 0; i < numPackets; i++) {
if (memcmp(PacketsIn[i], PacketsOut[i], T)
count++;
}
return count;
}