Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.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
fscanf正在以某种方式更改节点(在c中)_C_Trie_Scanf - Fatal编程技术网

fscanf正在以某种方式更改节点(在c中)

fscanf正在以某种方式更改节点(在c中),c,trie,scanf,C,Trie,Scanf,我是c语言的新手,已经被这个错误困扰了好几个小时。我的代码从txt文件中读取每个单词,然后将该单词存储在trie中的一个节点中 我已将问题缩小到用星号标出的区域: 此时,我已成功地将第一个单词添加到trie中,并检查正确节点的值是否与单词匹配。然后,我使用fscanf将文件中的下一个单词保存为字符“add”。然后,打印出与之前完全相同的内容,节点的字应该保持不变。然而,它不知何故被更改为刚从文件中读取的新词 这怎么可能呢 int main(int argc, char** argv) { tr

我是c语言的新手,已经被这个错误困扰了好几个小时。我的代码从txt文件中读取每个单词,然后将该单词存储在trie中的一个节点中

我已将问题缩小到用星号标出的区域: 此时,我已成功地将第一个单词添加到trie中,并检查正确节点的值是否与单词匹配。然后,我使用
fscanf
将文件中的下一个单词保存为字符“add”。然后,打印出与之前完全相同的内容,节点的字应该保持不变。然而,它不知何故被更改为刚从文件中读取的新词

这怎么可能呢

int main(int argc, char** argv) {

trie_t* trie = malloc(sizeof(trie_t));
trie_init(trie);

int ret;
char* add = malloc(128);
FILE* file = fopen(argv[5], "r");
if (file == NULL) {
    /* Failed to open the file for reading */
    return 0;
}

while (1) {*********************
    if (trie->head->children != NULL) {
        printf("%s\n", trie->head->children->word);
    }
    ret = fscanf(file, "%s", add);
    //printf("word = %s\n",toAdd);
    if (trie->head->children != NULL) {
        printf("%s\n", trie->head->children->word);
    }****************************
    if (ret == EOF) {
        /* End of file */
        ret = 1;
        break;
    } else if (ret <= 0) {
        printf("fails");
        /* Failed to read a word from the file */
        break;
    } else {
        printf("gets here\n");
        /* Succesfully read a word */
        int x = trie_add(trie, add);
        printf("%d\n",x);
    }
}
int main(int argc,char**argv){
trie_t*trie=malloc(sizeof(trie_t));
trie_init(trie);
int ret;
char*add=malloc(128);
FILE*FILE=fopen(argv[5],“r”);
if(file==NULL){
/*无法打开文件进行读取*/
返回0;
}
而(1){*********************
if(trie->head->children!=NULL){
printf(“%s\n”,trie->head->children->word);
}
ret=fscanf(文件“%s”,添加);
//printf(“word=%s\n”,toAdd);
if(trie->head->children!=NULL){
printf(“%s\n”,trie->head->children->word);
}****************************
如果(ret==EOF){
/*文件结束*/
ret=1;
打破

}否则如果(ret我假设您将指针
add
存储在
trie\u t
函数中。在这种情况下,由于您正在重复使用相同的内存位置
add
来读取下一个字符串,因此
add
所指向的指针的内容会发生变化。因为您只是将此指针存储在
trie中e> 该节点的内容也因此而更改。要解决此问题,您需要在使用
fscanf

从文件中读取新字符串之前,使用
malloc
再次分配内存。您只需在以下位置为
add
分配一次内存:

char* add = malloc(128);
您需要为每个单词分配内存,也就是说,您需要将
malloc
移动到您的读取周期

代码在发布时所做的是:一次分配128字节,然后每次
scanf()
时一次又一次地覆盖该内存空间


另外,在
char*add=malloc(128);
中,您应该将其指定为
char*add=malloc(128*sizeof(char));
只是为了清晰和便于携带:)

虽然我在
malloc()调用中建议使用
sizeof
,但请注意
sizeof(char)
是1。始终。在所有平台上。因此,在这里,它确实没有太大的改进作用。为了清晰起见,您可以使用
char*add=malloc(128*sizeof*add);
,这将是我的首选形式。