我正在尝试在哈希表中添加给定URL的所有前缀。我已将所有URL存储在文件中,现在从文件中读取URL。
Ex:对于下面给出的URL
com/google/www/
com/google/maps/
它应在表格中插入以下前缀
com/
com/google/
com/google/maps/
com/google/www/
以下是代码:
char* file = "path to file";
FILE *fd = fopen(file, "r");
if(fd == NULL) {
debug("some problem in opening the file !!!\n");
return;
}
while(!feof(fd)) {
char url[128];
memset(url, 0, 128);
int retrn = fscanf(fd, "%s", url);
//if retrn is positive
for(int i = 0 ; i < strlen(url); i++) {
if(url[i] == '/') {
char key[32];
memset(key, 0 , 32);
memcpy(key, url, i+1);
// Insert in hash table, check before insering if already present.
// Using DPDK functions for this.
if(rte_hash_lookup(hash_table, key) < 0)
rte_hash_add_key(hash_table, key);
}
}
}
但它正在插入
com/
com/google/
com/google/maps/
com/
com/google/
com/google/www/
然而,当我将关键字符数组的大小更改为128时,它工作正常。
char key[128];
memset(key, 0 , 128);
memcpy(key, url, i+1);
我不确定我缺少什么以及为什么当大小相同且哈希值不同时它会给出相同的哈希值。