我正在尝试在哈希表中添加给定 URL 的所有前缀。我已将所有 URL 存储在一个文件中,现在从文件中读取 URL。
例如:对于下面给出的 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);
我不确定我错过了什么,以及为什么当大小相同时它给出相同的哈希值,否则它会给出不同的哈希值。