2017-05-30 90 views
0

我編寫了一個C代碼來實現使用鏈接列表的字典(節點按排序順序)我想將數據保存到文件並能夠在數據重新加載時我下次運行這個程序。我無法從文件加載數據。這裏是我的讀取和寫入數據的代碼:從文件讀取鏈接列表並加載數據

struct node{ 
    char word[20]; 
    char meaning[5][100]; //2D array for saving multiple meanings of a word 
    struct node *next; 
}; 

void ReadData(struct node *head) 
{ 
    struct node *tail; 
    FILE *fp = fopen("dictionary.data", "rb"); 
    if(fp == NULL) 
    { 
     printf("Error opening file..\n"); 
     return; 
    } 
    while(!feof(fp)) 
    { 
     tail = (struct node*)calloc(1, sizeof(struct node)); 
     fread(tail->word, sizeof(head->word), 1, fp); 
     fread(tail->meaning, sizeof(head->meaning), 1, fp); 
     if(head == NULL) //for fresh run head is initialized with NULL 
     { 
      tail->next = head; 
      head = tail; 
     } 
     else 
     { 
      head->next = tail; 
      head = head->next; 
     } 
    } 
    fclose(fp); 
} 

我無法從文件到鏈接列表加載數據。代碼不起作用。我想不通問題出在哪裏..
這裏是我如何將數據寫入文件:

/*I think this code is working because size of the file increases after running the code*/ 
void WriteData(struct node *head) 
{ 
    FILE *fp = fopen("dictionary.data", "wb"); 
    if(fp == NULL) 
    { 
     printf("Error opening file..\n"); 
     return; 
    } 
    while(head != NULL) 
    { 
     fwrite(head->word, sizeof(head->word), 1, fp); 
     fwrite(head->meaning, sizeof(head->meaning), 1, fp); 
     head = head->next; 
    } 
    fclose(fp); 
} 

我用sizeof,而不是strlen,這是一個字符串。它最後會有空字符 - 沒有問題的字符串。它會消耗更多的內存。

+0

,一個是不讀..它是將數據寫入到下一個節點。讀取將通過'ReadData'中的'fread' – surjit

+0

完成讀取由'fread'完成if-else'部分用於修復節點 – surjit

+0

在'else'部分'head-> next = tail'之間的鏈接指定新節點的地址添加到'head'指向的節點的'next'文件中。現在我們必須將頭指向新節點(尾),新節點的地址被分配給前一個節點的下一個字段('head-> next = tail')。因此,爲了將'head'指向新節點,我使用'head = head-> next'作爲'head-> next' ='tail'的地址。 – surjit

回答

2

嘗試這種(未經測試):

void ReadData(struct node **head){//or struct node *ReadData(void){ ... return head; } 
    struct node temp = { .next = NULL };//{ {0}, {{0}}, NULL} 
    struct node *hp, *curr; 
    FILE *fp = fopen("dictionary.data", "rb"); 

    if(fp == NULL){ 
     printf("Error opening file..\n"); 
     return; 
    } 

    hp = *head; 
    while(fread(temp.word, sizeof(temp.word), 1, fp) && fread(temp.meaning, sizeof(temp.meaning), 1, fp)){ 
     struct node *np = malloc(sizeof(*np)); 
     if(np == NULL){ 
      perror("couldn't make new node by malloc:"); 
      return ;//release list 
     } 
     *np = temp; 

     if(hp == NULL){//for fresh run head is initialized with NULL 
      curr = hp = np; 
     } else {//If *head isn't NULL, you need to move to the last node first. 
      curr = curr->next = np; 
     } 
    } 
    fclose(fp); 
    *head = hp; 
} 
//.................... 
int main(void){ 
    //... 
    struct node *head = NULL; 
    //... 
    ReadData(&head); 
    //... 
+0

謝謝..這很完美 – surjit

+0

'void' functio n不能返回NULL。如果'malloc()'失敗,則必須將所有指針傳遞給'free()',否則會出現大量內存泄漏。 – user3629249

+0

@ user3629249是的,這是我的錯誤,Null的回報是。 謝謝你指出。 我只是在Malloc失敗時指出了這個列表的發佈,因爲它並不是一箇中心例子。 – BLUEPIXY

相關問題