c - 我的链表未填充 C

标签 c file struct linked-list

我正在尝试填充链接列表(dic)。

dataPtr 将是来自 kitap.txt 文件的一个单词,ID 将依次为 0,1,2。

我无法解决这个问题,你能看出什么问题吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define WORD_LENGHT 30

typedef struct numnode//MY STRUCTURE
{
    char dataPtr[1000];
    int ID;
    struct numnode* next;

} NUMNODE;

typedef struct num
{
   NUMNODE* head;
} NUM;

//////
//////
//////

int main()
{
    char word[1000];
    int total=0;
    FILE *test= fopen("kitap.txt", "r");

    NUM* dic;
    dic=(NUM*)malloc(sizeof(NUM));
    NUMNODE* temp;
    temp=dic->head;
    temp=malloc(sizeof(NUMNODE));

    while (!feof(test))//getting all words and trying to fill linkedlist
    {
        fscanf (test, "%s",word);
        strcpy(temp->dataPtr,word);//filling dataPtr part
        temp->ID=total;//filling ID
        printf("%d-%s\n",temp->ID,temp->dataP1tr);//i can print here but its not filling
        total++;
        temp=temp->next;
        temp=malloc(sizeof(NUMNODE));
    }

    //printf("-----------");
    FILE*file= fopen("kitap.txt", "r");
    //FILE*dictionary=fopen("dictionary.txt","w");

    temp=dic->head;//point to head
    /*while(!feof(file))//trying to print linked list
    {

        printf("%d-%s\n",temp->ID,temp->dataPtr); //it only prints head node
        temp=temp->next;
    }       
     return;
}

最佳答案

尝试交换这两条指令:

temp=temp->next;
temp=malloc(sizeof(NUMNODE));

有这些:

temp->next=malloc(sizeof(NUMNODE));
temp=temp->next;

关于c - 我的链表未填充 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23854254/

相关文章:

c - srand 函数是否适用于多个翻译单元?

c - 如何在嵌入式 C 中读取来自 GSM 调制解调器的消息?

file - 为什么程序要/偏爱使用稀疏文件?

c - C 中的结构作为函数返回值

c++ - 结构中的数组

c++ - 2 昏暗数组和双指针

C++:以最简单和最干净的方式将值写入文件?

java - 尝试在对象类中创建一个将文本文件划分为多个部分的函数(Java)

c - 如何在 C 中使用结构体和指针的指针中的索引

c - 很难用函数打印结构成员