Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 尝试使用循环设置哈希表中的新元素时失败_C - Fatal编程技术网

C 尝试使用循环设置哈希表中的新元素时失败

C 尝试使用循环设置哈希表中的新元素时失败,c,C,我正在尝试设置包含新元素的哈希表: 因此,我创建了一个数组ligne[3][25]={NULL},并且: strcpy(ligne[0] , "mama"); strcpy(ligne[1] , "kaka"); strcpy(ligne[2] , "tata"); 使用ligne将元素添加到哈希表中时:有效:) 文本文件dico.txt包含: mama kaka tata 我添加元素的函数是ht\u集(哈希表,元素) * Insert a value into a hash table.

我正在尝试设置包含新元素的哈希表: 因此,我创建了一个数组
ligne[3][25]={NULL}
,并且:

strcpy(ligne[0] , "mama");
strcpy(ligne[1] , "kaka");
strcpy(ligne[2] , "tata");
使用ligne将元素添加到哈希表中时:有效:)

文本文件dico.txt包含:

mama
kaka
tata
我添加元素的函数是ht\u集(哈希表,元素)

* Insert a value into a hash table. */
entry_t* ht_set( hashtable_t *hashtable, char *key ) {
    int bin = 0;
    entry_t *newpair = NULL;
    entry_t *next = NULL;
    entry_t *last = NULL;

    bin = ht_hash( hashtable, key );

    next = hashtable->table[ bin ];

    while( next != NULL && next->key != NULL ) {
        last = next;
        next = next->next;
    }

    /* There's already a pair.  Let's replace that string. */
    if( next != NULL && next->key != NULL) {

        free( next->key );
        next->key = strdup( key );
        return next;


    /* Nope, could't find it.  Time to grow a pair. */
    } else {
        newpair = ht_newpair( key );

        /* We're at the start of the linked list in this bin. */
        if( next == hashtable->table[ bin ] ) {
            newpair->next = next;
            hashtable->table[ bin ] = newpair;

        /* We're at the end of the linked list in this bin. */
        } else if ( next == NULL ) {
            last->next = newpair;

        /* We're in the middle of the list. */
        } else  {
            newpair->next = next;
            last->next = newpair;
        }
        return newpair;
    }
}
int main(int argc,char* argv []){
    hashtable_t* hashtable = ht_create(62400);
    int i=0;
    char ligne[3][25]= {NULL}; // three char pointer that take each ligne
    FILE* file = fopen("dico.txt", "r");
    if(file != NULL){
        while (fgets(ligne[i], 25, file) != NULL ){
            i++;
        }

        for(i = 0 ; i < 3 ; i++){
            ht_set(hashtable, ligne[i] );
        }
        printf("%s ", ht_get(hashtable , "mama"));// Search "mama" in the hashtable if exist then write it if not exist then printf (null)
    }
    free(hashtable);
    fclose(file);
    return  0;
}
我的代码是:

* Insert a value into a hash table. */
entry_t* ht_set( hashtable_t *hashtable, char *key ) {
    int bin = 0;
    entry_t *newpair = NULL;
    entry_t *next = NULL;
    entry_t *last = NULL;

    bin = ht_hash( hashtable, key );

    next = hashtable->table[ bin ];

    while( next != NULL && next->key != NULL ) {
        last = next;
        next = next->next;
    }

    /* There's already a pair.  Let's replace that string. */
    if( next != NULL && next->key != NULL) {

        free( next->key );
        next->key = strdup( key );
        return next;


    /* Nope, could't find it.  Time to grow a pair. */
    } else {
        newpair = ht_newpair( key );

        /* We're at the start of the linked list in this bin. */
        if( next == hashtable->table[ bin ] ) {
            newpair->next = next;
            hashtable->table[ bin ] = newpair;

        /* We're at the end of the linked list in this bin. */
        } else if ( next == NULL ) {
            last->next = newpair;

        /* We're in the middle of the list. */
        } else  {
            newpair->next = next;
            last->next = newpair;
        }
        return newpair;
    }
}
int main(int argc,char* argv []){
    hashtable_t* hashtable = ht_create(62400);
    int i=0;
    char ligne[3][25]= {NULL}; // three char pointer that take each ligne
    FILE* file = fopen("dico.txt", "r");
    if(file != NULL){
        while (fgets(ligne[i], 25, file) != NULL ){
            i++;
        }

        for(i = 0 ; i < 3 ; i++){
            ht_set(hashtable, ligne[i] );
        }
        printf("%s ", ht_get(hashtable , "mama"));// Search "mama" in the hashtable if exist then write it if not exist then printf (null)
    }
    free(hashtable);
    fclose(file);
    return  0;
}
intmain(intargc,char*argv[]){
hashtable_t*hashtable=ht_create(62400);
int i=0;
char ligne[3][25]={NULL};//获取每个ligne的三个char指针
FILE*FILE=fopen(“dico.txt”、“r”);
如果(文件!=NULL){
while(fgets(ligne[i],25,file)!=NULL){
i++;
}
对于(i=0;i<3;i++){
ht_集(hashtable,ligne[i]);
}
printf(“%s”,ht_get(哈希表,“mama”);//如果存在,则在哈希表中搜索“mama”,如果不存在则写入,然后printf(null)
}
自由(哈希表);
fclose(文件);
返回0;
}

谢谢你的帮助,这花了我5天的时间,我真的无法解决这个问题。LOL LOL

上面的代码似乎没有显示错误,或者从中看不出明显的错误。请发一张MCVE。请参阅:。“增加循环时读入另一个变量(如
cnt
)的字符串数,并在
for
循环中使用它。看起来您在
中错误地使用了
i
,而在
之后是
for
循环,您在不知道发生了什么的情况下更改了某些内容。上面的代码似乎没有显示错误,或者从中看不明显。请发一张MCVE。请参阅:.“将while循环中读取的字符串数增加到另一个变量中,如
cnt
,并在
for
循环中使用它。看起来您在
while
for
循环中都错误地使用了
i
,您在不知道是什么的情况下更改了某些内容。”。。
int main(int argc,char* argv []){
    hashtable_t* hashtable = ht_create(62400);
    int i=0;
    char ligne[3][25]= {NULL}; // three char pointer that take each ligne
    FILE* file = fopen("dico.txt", "r");
    if(file != NULL){
        while (fgets(ligne[i], 25, file) != NULL ){
            i++;
        }

        for(i = 0 ; i < 3 ; i++){
            ht_set(hashtable, ligne[i] );
        }
        printf("%s ", ht_get(hashtable , "mama"));// Search "mama" in the hashtable if exist then write it if not exist then printf (null)
    }
    free(hashtable);
    fclose(file);
    return  0;
}