簡體   English   中英

C-如何從二進制文件中讀取鏈接列表?

[英]C - How to Read in a Linked List from a Binary File?

我的代碼需要一些幫助。 我已經創建了用於將鏈接列表寫入二進制文件的函數。 現在,我嘗試從我的寫函數已輸出的二進制文件中讀取鏈表。 我試圖讀取二進制文件並創建鏈接段錯誤的嘗試。 這是我在下面的嘗試。

我究竟做錯了什么?

void readlist(struct link **headptr) {                                                                                                                      
    FILE *text = fopen("numbers.bin", "rb");                                                                                                                 
    struct link *head = *rootptr;                                                                                                                                                                                                                                                           


   while (head->next != NULL) {                                                                                                                             
       struct link *newlink = (struct link*) malloc(sizeof(struct link));                                                                                    
       fread(&newlink->val, sizeof(int), 1, text);                                                                                                       
       head->next = newlink;                                                                                                                                 
       head = newlink;                                                                                                                                       
    }                                                                                                                                                        
       fclose(text);   
}                                                                                                                                                                                                                                                                              

我希望這段代碼對您有所幫助。 如果使用全局變量headptr,它將更加簡單。

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <unistd.h>
#include <fcntl.h>

#define LISTSAVE "numbers.bin"

struct link {
    int val ;
    struct link *next ;
} ;

void print_list(struct link *headptr) {
    struct link *tmp = headptr->next ;
    int cnt=0 ;
    printf("---list start----\n") ;
    while(tmp) {
        printf("%d ", tmp->val) ;
        cnt++ ;
        tmp=tmp->next ;
    }
    printf("\n%d items\n", cnt) ;
    printf("---list end----\n") ;
}


void add_first(struct link *headptr, int val) {
    struct link *tmp = malloc(sizeof(struct link)) ;
    tmp->val = val ;
    tmp->next = headptr->next ;
    headptr->next = tmp ;
}

void add_tail(struct link *headptr, int val) {
    struct link *tmp = headptr;
    struct link *tmp2 = malloc(sizeof(struct link)) ;
    tmp2->val = val ;
    tmp2->next=NULL ;

    while(tmp->next) {
        tmp=tmp->next ;
    }
    tmp->next=tmp2 ;
}

void del_list(struct link *headptr) {
    struct link *tmp = headptr->next ;
    struct link *tmp2 = NULL ;
    while (tmp) {
        tmp2=tmp->next ;
        free(tmp);
        tmp=tmp2 ;
    }
    headptr->next=NULL ;
}

void save_list(struct link *headptr) {
    FILE *text = fopen(LISTSAVE, "wb+") ;
    struct link *tmp=headptr->next ;
    int cnt=0 ;

    if ( text==NULL || headptr==NULL ) {
        printf("filed to save.\n") ;
        return ;
    }
    while (tmp!=NULL ) {
        cnt++ ;
        fwrite(&tmp->val, sizeof(int), 1, text) ;
        tmp = tmp->next ;
    }
    fclose(text) ;
    printf("write %d items ok\n", cnt) ;
}

void read_list(struct link *headptr) {
    FILE *text = fopen(LISTSAVE, "rb") ;
    int val ;
    int cnt=0 ;
    while( fread(&val, sizeof(int), 1, text) > 0 ) {
        add_tail(headptr, val) ;
        cnt++ ;
    }
    fclose(text);
    printf("read %d items ok\n", cnt) ;
}


int main() {
    struct link head ;
    head.val=0 ;
    head.next=NULL ;

    add_first(&head, 40) ;
    add_first(&head, 30) ;
    add_first(&head, 20) ;
    add_first(&head, 10) ;
    add_tail(&head, 50) ;
    add_tail(&head, 60) ;

    print_list(&head) ;
    printf("--save list\n") ;
    save_list(&head) ;
    del_list(&head) ;

    printf("--read list\n") ;
    read_list(&head) ;
    print_list(&head) ;
    del_list(&head) ;

    return 0 ;
}

輸出是這個

---list start----
10 20 30 40 50 60 
6 items
---list end----
--save list
write 6 items ok
--read list
read 6 items ok
---list start----
10 20 30 40 50 60 
6 items
---list end----

您無法將通用鏈接列表保存到文件。 為了使其正常工作,您需要完全控制內存分配,包括獲得的地址。 通常,您不會。

如果將鏈接列表覆蓋在單個緩沖區上,則可以通過將指針轉換為索引然后返回(或僅通過使用索引而不是指針)來保存它,但是一般來講,您不能將鏈接列表保存到磁盤上,而要釋放內存( (包括退出),重新加載,然后期望保存的指針起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM