下面的文件写入代码有什么问题吗?
我有以下结构,用于创建指纹哈希表
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜
WriteHTtoFile
中的循环应该看起来更像这样:I guess the loop in
WriteHTtoFile
should look more like this:你的问题还不止这些;这段代码是无可救药地拙劣的
%d
格式打印出fing_print
;根本不清楚fing_print
是什么/应该是什么(二进制字符串;ascii 字符串)。阅读一本关于 C 的好书,并进行一些调试器的练习。
You have more problems than that; this code is hopelessly botched
%d
format to print outfing_print
; it's not at all clear whatfing_print
is/should be (binary string; ascii string).Get a good book on C, and get some practice with a debugger.