我试图填写一个链接列表(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-%sn",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-%sn",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;