小编jca*_*eup的帖子

strncpy中的SEGMENTATION FAULT - 来自字典的加载

我有这个函数"加载",我从字典中读取单词并将它们放在链表的哈希表中.当我尝试读取一行并将其保存在我的new_node-> text中时,编译器返回SEGMENTATION FAULT,我不知道为什么.当我使用strncpy时,错误会出现.

#define HASHTABLE_SIZE 76801


typedef struct node
{
        char text[LENGTH+1];
        //char* text;
        //link to the next word
        struct node* next_word;
}
node;


    node* hashtable[HASHTABLE_SIZE];

    bool load(const char* dictionary)
    {
        FILE* file = fopen(dictionary,"r");
        unsigned long index = 0;
        char str[LENGTH+1];

        if(file == NULL)
        {
            printf("Error opening file!");
            return false;
        }

        while(! feof(file))
        {
            node * new_node = malloc(sizeof(node)+1000);


            while( fscanf(file,"%s",str) > 0)
            {
                printf("The word is %s",str);
                strncpy(new_node->text,str,LENGTH+1);
                //strcpy(new_node->text,str);

                new_node->next_word = NULL;
                index = hash( (unsigned char*)new_node->text);

                if(hashtable[index] …
Run Code Online (Sandbox Code Playgroud)

c load segmentation-fault strcpy strncpy

2
推荐指数
1
解决办法
425
查看次数

标签 统计

c ×1

load ×1

segmentation-fault ×1

strcpy ×1

strncpy ×1