下面的文件写入代码有什么问题吗?

发布于 2025-01-08 16:43:06 字数 2487 浏览 1 评论 0原文

我有以下结构,用于创建指纹哈希表

typedef struct fpinfo
{ 
   unsigned long chunk_offset;
   unsigned long chunk_length;
   unsigned char fing_print[33];
}fpinfo;

/* * 下面定义了哈希表中的一个条目。 */

typedef struct Hash_Entry 
{
   struct Hash_Entry *next; /* Link entries within same bucket. */
   unsigned namehash;   /* hash value of key */
   struct fpinfo fp;
} Hash_Entry;

typedef struct Hash_Table 
{
   struct Hash_Entry **bucketPtr;   /* Buckets in the table */
   int numBuckets;
   int        buck_entry_count[64];//number of entries in each bucket
   int      size;       /* Actual size of array. */
   int      numEntries; /* Number of entries in the table. */
   int      mask;       /* Used to select bits for hashing. */
} Hash_Table;

将指纹插入其中

int Hash_CreateEntry(Hash_Table *t, struct Hash_Entry he)
{
Hash_Entry *e;
const char *p;
int keylen;
struct Hash_Entry **hp;
unsigned long h = 0, g,i=0;

while ( i<5 ) 
 {
    h = ( h ) + he.fp.fing_print[i]++;
     g = h & 0xF0000000;
    h ^= g >> 24;
    h &= ~g;
    i++;
}

p =(const char*) he.fp.fing_print;
for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next)
{
    if (e->namehash == h && strcmp((const char *)(e->fp).fing_print, p) == 0)
    {
        printf("\n%d \t%s",(e->fp).chunk_length,(e->fp).fing_print);
        return (1);
    }
}

if (t->numEntries >= rebuildLimit * t->size)
    WriteHTtoFile(t);
e = (Hash_Entry *)malloc(sizeof(*e) /*+ keylen*/);
hp = &t->bucketPtr[h & t->mask];
e->next = *hp;
*hp = e;
e->namehash = h;
strcpy((char *)(e->fp).fing_print, p);
t->numEntries++;
t->buck_entry_count[h & t->mask]++;
return (0);
}

我使用将 HT 写入文件的代码

static void   WriteHTtoFile(Hash_Table *t)
{
Hash_Entry *e, *next = NULL, **hp, **xp;
int i=0, mask;
    Hash_Entry **oldhp;
int oldsize;
FILE *htfile=fopen("htfile.txt","a");
system("cls");

for ( hp = t->bucketPtr;t->bucketPtr!=NULL;hp=t->bucketPtr++)
    {
    for (e = *hp;e ->next!= NULL;e = e->next) 
    fprintf(htfile,"\n%d \t%s",(e->fp).chunk_length,(e->fp).fing_print);        
    }
fclose(htfile);
}

,我的问题是(是)

1 - 它显示“访问冲突读取位置 0xfdfdfe09”。写入相当多次后(写入了 6401 个指纹)。说明故障行为文件写入函数中的fprintf()。

2-它写的指纹和我写之前的指纹根本不匹配。实际上,编译器中指纹的十六进制表示(我使用的是VC2010)和程序读取的指纹是不同的。

3-所有条目的chunck_length值为3452816845l

i have the following structure that i use to create a hash table for fingerprints

typedef struct fpinfo
{ 
   unsigned long chunk_offset;
   unsigned long chunk_length;
   unsigned char fing_print[33];
}fpinfo;

/*
* The following defines one entry in the hash table.
*/

typedef struct Hash_Entry 
{
   struct Hash_Entry *next; /* Link entries within same bucket. */
   unsigned namehash;   /* hash value of key */
   struct fpinfo fp;
} Hash_Entry;

typedef struct Hash_Table 
{
   struct Hash_Entry **bucketPtr;   /* Buckets in the table */
   int numBuckets;
   int        buck_entry_count[64];//number of entries in each bucket
   int      size;       /* Actual size of array. */
   int      numEntries; /* Number of entries in the table. */
   int      mask;       /* Used to select bits for hashing. */
} Hash_Table;

I insert fingerprints into it using

int Hash_CreateEntry(Hash_Table *t, struct Hash_Entry he)
{
Hash_Entry *e;
const char *p;
int keylen;
struct Hash_Entry **hp;
unsigned long h = 0, g,i=0;

while ( i<5 ) 
 {
    h = ( h ) + he.fp.fing_print[i]++;
     g = h & 0xF0000000;
    h ^= g >> 24;
    h &= ~g;
    i++;
}

p =(const char*) he.fp.fing_print;
for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next)
{
    if (e->namehash == h && strcmp((const char *)(e->fp).fing_print, p) == 0)
    {
        printf("\n%d \t%s",(e->fp).chunk_length,(e->fp).fing_print);
        return (1);
    }
}

if (t->numEntries >= rebuildLimit * t->size)
    WriteHTtoFile(t);
e = (Hash_Entry *)malloc(sizeof(*e) /*+ keylen*/);
hp = &t->bucketPtr[h & t->mask];
e->next = *hp;
*hp = e;
e->namehash = h;
strcpy((char *)(e->fp).fing_print, p);
t->numEntries++;
t->buck_entry_count[h & t->mask]++;
return (0);
}

The Code I used to write the HT to file is

static void   WriteHTtoFile(Hash_Table *t)
{
Hash_Entry *e, *next = NULL, **hp, **xp;
int i=0, mask;
    Hash_Entry **oldhp;
int oldsize;
FILE *htfile=fopen("htfile.txt","a");
system("cls");

for ( hp = t->bucketPtr;t->bucketPtr!=NULL;hp=t->bucketPtr++)
    {
    for (e = *hp;e ->next!= NULL;e = e->next) 
    fprintf(htfile,"\n%d \t%s",(e->fp).chunk_length,(e->fp).fing_print);        
    }
fclose(htfile);
}

my problem is (are)

1-it says "Access violation reading location 0xfdfdfe09." after writting a considerable number of times (it wrote 6401 fingerprints). It indicates the faulty line to be the fprintf() in the file writing function.

2- the fingerprints it writes and what i have before writing does not match at all. Actually the hex representation of the fingerprints in the compiler(i am using VC2010) and the one i have which is read by the program are different.

3- the values for chunck_length of all the entries are 3452816845l

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻泪 2025-01-15 16:43:06

我猜 WriteHTtoFile 中的循环应该看起来更像这样:

for (i = 0; i < t->numBuckets; ++i)
{
    for (e = t->bucketPtr[i]; e && e->next; e = e->next) 
        fprintf(htfile, /*...*/);        
}

I guess the loop in WriteHTtoFile should look more like this:

for (i = 0; i < t->numBuckets; ++i)
{
    for (e = t->bucketPtr[i]; e && e->next; e = e->next) 
        fprintf(htfile, /*...*/);        
}
倾城泪 2025-01-15 16:43:06

你的问题还不止这些;这段代码是无可救药地拙劣的

  • WriteHTToFile 修改了原始哈希表,所以你最终至少会出现内存泄漏
  • 你使用 %d 格式打印出 fing_print;根本不清楚 fing_print 是什么/应该是什么(二进制字符串;ascii 字符串)。

阅读一本关于 C 的好书,并进行一些调试器的练习。

You have more problems than that; this code is hopelessly botched

  • WriteHTToFile modifies the original hashtable, so you end up with memory leak at least
  • You use %d format to print out fing_print; it's not at all clear what fing_print is/should be (binary string; ascii string).

Get a good book on C, and get some practice with a debugger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文