[英]How to compare two structs in c?
我知道,如果我想比较两个结构,而不是我必须为自己编写它,因为没有任何功能,但我无法弄清楚我该怎么做。 我有三个结构:primary,secondarystruct和difference(这应该包含不同的项目)。 这三个都有以下成员:char * filename,char * size,int size。
我需要的只是那些不在第二层结构中的物品,或者如果它们是那么我只需要它们的尺寸大于第二层结构的尺寸。 希望你明白我想要的。 我的英语不是最好的,对不起。
这是我试过的:
j = 0;
x = 0;
for ( i = 0; i < primarypcs; )
{
memset( tmp, 0, sizeof( tmp ) );
l = 1;
for ( k = 0; k < strlen( primary[i].filename );k++ )
{
tmp[k] = primary[i].filename[l];
l++;
}
tmp[k]='\0';
memset( buf, 0, sizeof( buf ) );
l = 1;
for ( k = 0; k < strlen( secondarystruct[j].filename ); k++ ) //<-- here is where my program freezes
{
buf[k] = secondarystruct[j].filename[l];
l++;
}
buf[k]='\0';
if ( ( stricmp( tmp, buf ) == 0 ) && ( x == 0 ) )
{
if ( primary[i].intsize > secondarystruct[j].intsize )
{
difference[diff].filename = strdup( primary[i].filename );
difference[diff].size = strdup( primary[i].size );
difference[diff].intsize = -1;
diff++;
i++;
if ( j == secondarypcs ) x = 1;
else j++;
}
else if ( x == 0 )
{
i++;
if ( j == secondarypcs ) x = 1;
else j++;
}
}
else
{
difference[diff].filename = strdup( primary[i].filename );
difference[diff].size = strdup( primary[i].size );
difference[diff].intsize = -1;
diff++;
i++;
}
}
请告诉我我做错了什么!
谢谢,kampi
更新:
对不起,看来,我没有给你足够的信息。 所以:两个结构都包含来自不同驱动器的文件列表,例如“C:\\”和“D:\\”。 这就是为什么我不能只使用简单的strcmp,因为第一个字母总是不同的。 这就是为什么我必须“切断它们”然后进行比较。 这个程序应该这样工作:它从c:\\检索文件列表,然后从d:\\检索文件列表,然后比较它们。 如果在c:\\上的文件在d:\\上不存在那么它应该被复制到那里,如果在d:\\上有一个文件在c:\\上不存在那么它应该被忽略(我不要不管怎么说都没事。 如果一个文件在c:\\和d:\\中找到,那么我不想复制它,如果来自c:\\的文件大于d:\\上的文件大小
希望你现在明白我想要什么。
“冻结”最可能的原因是strlen()
调用,这可能是由某些内存问题引起的(即,它的参数不是指向以零结尾的字符串的指针)。 可能有多种原因:
tmp
或buf
)覆盖了一些内存。 x
作为指标你已经走到了尽头,但之后你使用了secondarystruct[j]
。 此外,如果secondarypcs
与primarypcs
具有相同的含义,即数组元素的数量,那么您使用的是secondarystruct[secondarypcs]
,但这是超出范围的。 其他一些提示:
primarypcs
缺少secondarypcs
的第一个文件, primarypcs
,您的代码都会将所有内容都放到diff中。 无论第一个字母如何比较字符串都可以这样做:
(* str1 && * str2?strcmp(str1 + 1,str2 + 1): - 1)
我建议像这样的代码:
void add_to_difference(struct diff_file* f);
...
// assuming primarystruct and secondarystruct are arrays of diff_file sorted by filename
j=0;
for(i=0; i<primarypcs; i++) {
// find a passibly matching secondary file
while(j<secondarypcs && strcmp(primarystruct[i].filename+1, secondarystruct[j].filename+1)<0)
j++;
// not found... add all overflow items to diff
if(j>=secondarypcs) {
for(; i<primarypcs; i++)
add_to_diff(primarystruct+i);
break;
}
// do the comparison
if(strcmp(primarystruct[i].filename+1, secondarystruct[j].filename+1)>0 ||
primarystruct[i].intsize>secondarystruct[j].intsize)
add_to_diff(primarystruct+i);
// that's it
}
如果你有相同的结构变量。 访问struct变量的每个成员,然后进行适当的比较。 您需要根据数据类型比较值。
你可以使用memcmp()函数比较结构。 如果在memcmp中指定结构及其长度,它将比较并以整数形式返回结果,如strcmp。
但它比strcmp()函数更好。 它直接处理内存。 所以它可以准确地给出结果。
您必须逐字段比较结构。 将strcmp用于char *字符串和比较运算符用于整数。 通常你会为比较创建一个单独的函数,如果结构相同,函数将返回0,如果第一个是“较小”则返回负值,如果第一个更大则返回正值。
如果你只想完成你所说的话,那就太多了。
只需使用strcmp()来比较文件名和大小,使用“>”来表示大小。 然后根据比较结果,您可以使用strdup()或简单的“=”复制适当的成员到差异结构。
if (!strcmp(first.filename, second.filename) { ...what to do, when they are different... } if (!strcmp(first.size, second.size) { ...what to do, when they are different... } if (first.intsize != second.intsize) { ..etc... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.