我正在写一个程序,按照翻译的路径来翻译一个给定的单词。
给定单词的每个字母代表一个特定的节点。
输出结果应该是所有要翻译的单词,然后是相应的翻译,但是我接收到的是输出,而不是:

a: 4��t�    xp� t����
an: 4��t�   xp� t����
ant: 4��t�  xp� t����
at: 4��t�   xp� t����
atom: 4��t� xp� t����
no: 4��s�   xp� t����
not: 4��s�  xp� t����
tea: 4��q�  xp� t����
ten: 4��q�  xp� t����

主要c:
    int main()
    {
        struct Trie* trie = trie_alloc();
        trie_insert_from_file(trie, "dictionary.txt");
        trie_print_mappings(trie);
        trie_free(trie);

        return 0;
    }

特里亚.c:
   int trie_insert(Trie* trie, const char* key, const char* value)
    {
        ...
    }

    void trie_insert_from_file(Trie* trie, const char* file_name)
    {
        FILE* file = fopen(file_name, "r");
        if (file == NULL)
        {
            fprintf(stderr, "Unable to open %s for reading: %s\n",
                    file_name, strerror(errno));
            return;
        }
        while (!feof(file))
        {
            char key[64];
            char value[64];
            int nb_matched = fscanf(file, "%63[a-z] : %63[a-z]\n", key, value);

            if (nb_matched == 2)
            {
                  trie_insert(trie, key, value);
            }
            else
            {
                fprintf(stderr, "Syntax error while reading file\n");
                fclose(file);
                return;
            }
        }
        fclose(file);
    }

    static char* str_append_char(const char* str, char c)
    {
        size_t len =  strlen(str);
        char* new_str = malloc(len + 2);
        strcpy(new_str, str);
        new_str[len] = c;
        new_str[len + 1] = '\0';
        return new_str;
    }

    static void trie_print_mappings_aux(Trie* trie, const char* current_prefix)
    {
        if (trie->value != NULL)
            printf("%s: %s\n", current_prefix, trie->value);
        int i;
        for (i = 0; i < TRIE_NB_CHILDREN; i++)
        {
            Trie* child = trie->children[i];
            if (child != NULL)
            {
                char* child_prefix =
                    str_append_char(current_prefix, trie_get_child_char(i));
                trie_print_mappings_aux(child, child_prefix);
                free(child_prefix);
            }
        }
    }

    void trie_print_mappings(Trie* trie)
    {
        trie_print_mappings_aux(trie, "");
    }

特里亚:
#define TRIE_NB_CHILDREN 26

typedef struct Trie
{
    char* value;
    struct Trie* children[TRIE_NB_CHILDREN];
} Trie;

当我使用函数trie_insert手动插入数据而不从文件中读取带有insert_的.txt文件时,不会发生这种情况。
Eg. trie_insert(trie, (const char*)&"ten", (const char*)&"tien");
    trie_insert(trie, (const char*)&"no", (const char*)&"nee");
    trie_insert(trie, (const char*)&"not", (const char*)&"niet" );
...

经过一些研究,我相信这可能与我的写作超出了允许的记忆位置有关。但我不知道哪里出了问题。
函数insert_from_file、str_append_char、trie_print_mapping_aux*应该正常工作,因为它们是给我的。所以错误可能在trie_insert中,我实现的那个。
任何帮助都将不胜感激。

最佳答案

您正在将trie->value指针(intrie_insert)设置为堆栈上存储的字符串(intrie_insert_from_file-char value[64])。
当函数返回时,堆栈中存储的变量会“丢失”,这意味着trie指向了不应该指向的地方(在同一个指针地址上会有完全不同和不相关的数据)。
解决方案是将value字符串复制到堆:

if(*key == '\0')
{
    trie->value = (char*)malloc(sizeof(char)*64);
    strcpy(trie->value, value);
    return 1;
}

只要确保在不需要的时候释放分配的内存。

07-24 09:51
查看更多