简体   繁体   English

如何比较c中的两个结构?

[英]How to compare two structs in c?

I know, that if i want to compare two structs than i have to write it for myself, because there isn't any function for this, but i can't figure out how should i do that. 我知道,如果我想比较两个结构,而不是我必须为自己编写它,因为没有任何功能,但我无法弄清楚我该怎么做。 I have three structs : primary, secondarystruct, and difference(this should contain the different items). 我有三个结构:primary,secondarystruct和difference(这应该包含不同的项目)。 All three has the following members : char * filename, char * size, int size. 这三个都有以下成员:char * filename,char * size,int size。

All i need are those items which aren't in the secondarystruct , or if they are then i need them only if their size is bigger then the secondarystruct's size. 我需要的只是那些不在第二层结构中的物品,或者如果它们是那么我只需要它们的尺寸大于第二层结构的尺寸。 Hope you understand what i want. 希望你明白我想要的。 My english isn't the best, sorry for this. 我的英语不是最好的,对不起。

Here is what i tried: 这是我试过的:

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++;
    }
}

Please tell me what i'm doing wrong! 请告诉我我做错了什么!

Thanks, kampi 谢谢,kampi

Update: 更新:

Sorry, it seems, i gave you not enough information. 对不起,看来,我没有给你足够的信息。 So: both structures are containing file list, from different drives, like "C:\\" and "D:\\". 所以:两个结构都包含来自不同驱动器的文件列表,例如“C:\\”和“D:\\”。 This is the reason why i can't use just simple strcmp, because the first letter will always differ. 这就是为什么我不能只使用简单的strcmp,因为第一个字母总是不同的。 That's why i have to "cut them off" and then compare. 这就是为什么我必须“切断它们”然后进行比较。 This program should work like this: It retrieves the file list from c:\\ and then retrieves the filelist from d:\\ and then compares them. 这个程序应该这样工作:它从c:\\检索文件列表,然后从d:\\检索文件列表,然后比较它们。 If on file which is on c:\\ doesn't exists on d:\\ then it should be copied there, if on d:\\ there is a file which doesn't exists on c:\\ then it should be ignored(i don't wan't to do with it anything). 如果在c:\\上的文件在d:\\上不存在那么它应该被复制到那里,如果在d:\\上有一个文件在c:\\上不存在那么它应该被忽略(我不要不管怎么说都没事。 If a file which is found in c:\\ and d:\\ as well, then i wan't to copy it only then if the file from c:\\ has a bigger size than a file which is on d:\\ 如果一个文件在c:\\和d:\\中找到,那么我不想复制它,如果来自c:\\的文件大于d:\\上的文件大小

Hope you understand now what i want. 希望你现在明白我想要什么。

The most probable cause of your "freeze" is the strlen() call, which is likely caused by some memory problem (ie. that its argument isn't a pointer to string terminated by zero). “冻结”最可能的原因是strlen()调用,这可能是由某些内存问题引起的(即,它的参数不是指向以零结尾的字符串的指针)。 There may be multiple causes of this: 可能有多种原因:

  • you may have overwritten some of your memory by a buffer overflow (of tmp or buf ). 你可能已经通过缓冲区溢出( tmpbuf )覆盖了一些内存。
  • I assume you're using x as an indicator you've gone to the end, but you use secondarystruct[j] after that. 我假设你使用x作为指标你已经走到了尽头,但之后你使用了secondarystruct[j] Moreover, if secondarypcs has the same meaning as primarypcs , that is, the count of elements of an array, you're using secondarystruct[secondarypcs] , but that's out-of-bounds. 此外,如果secondarypcsprimarypcs具有相同的含义,即数组元素的数量,那么您使用的是secondarystruct[secondarypcs] ,但这是超出范围的。

Some other tips: 其他一些提示:

  • if the first file in secondarypcs is missing in primarypcs , your code will put everything to diff, no matter what. 如果primarypcs缺少secondarypcs的第一个文件, primarypcs ,您的代码都会将所有内容都放到diff中。
  • comparing the strings regardless of the first letter can be done like this: 无论第一个字母如何比较字符串都可以这样做:

    (*str1 && *str2 ? strcmp(str1+1, str2+1) : -1) (* str1 && * str2?strcmp(str1 + 1,str2 + 1): - 1)

I'd suggest code like this: 我建议像这样的代码:

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
}

If you have same structure variables. 如果你有相同的结构变量。 Access each member of the struct variables , Then do appropriate comparison . 访问struct变量的每个成员,然后进行适当的比较。 you need to compare the values based on their data types. 您需要根据数据类型比较值。

you can compare the structures using memcmp() function. 你可以使用memcmp()函数比较结构。 If you specify the structures and its length in the memcmp it will compare and returns the results in integer as like strcmp. 如果在memcmp中指定结构及其长度,它将比较并以整数形式返回结果,如strcmp。

But It is better than the strcmp() function. 但它比strcmp()函数更好。 It directly deals with memory. 它直接处理内存。 so it can give results accurately. 所以它可以准确地给出结果。

You must compare structs field by field. 您必须逐字段比较结构。 Use strcmp for char* strings and comparison operators for integers. 将strcmp用于char *字符串和比较运算符用于整数。 Normally you would make a separate function for comparing and the function would return 0 if the structs are the same, a negative value if the first one is "smaller" and a positive value if the first one is bigger. 通常你会为比较创建一个单独的函数,如果结构相同,函数将返回0,如果第一个是“较小”则返回负值,如果第一个更大则返回正值。

This is too much code if you just want to accomplish what you say. 如果你只想完成你所说的话,那就太多了。

Just use strcmp() to compare filenames and sizes, and ">" for size. 只需使用strcmp()来比较文件名和大小,使用“>”来表示大小。 Then basing on the result of comparison, you may copy appriopriate members to difference structure using strdup() or simple "=" for integers. 然后根据比较结果,您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM