簡體   English   中英

如何比較c中的兩個結構?

[英]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()調用,這可能是由某些內存問題引起的(即,它的參數不是指向以零結尾的字符串的指針)。 可能有多種原因:

  • 你可能已經通過緩沖區溢出( tmpbuf )覆蓋了一些內存。
  • 我假設你使用x作為指標你已經走到了盡頭,但之后你使用了secondarystruct[j] 此外,如果secondarypcsprimarypcs具有相同的含義,即數組元素的數量,那么您使用的是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM