C 如何从二进制文件中读取链表?

C 如何从二进制文件中读取链表?,c,singly-linked-list,C,Singly Linked List,我需要一些代码方面的帮助。我创建了将链表写入二进制文件的函数。现在,我正试图从写入函数输出的二进制文件中读取一个链表。我试图读入二进制文件并创建链接。下面是我的尝试 我做错了什么 void readlist(struct link **headptr) {

我需要一些代码方面的帮助。我创建了将链表写入二进制文件的函数。现在,我正试图从写入函数输出的二进制文件中读取一个链表。我试图读入二进制文件并创建链接。下面是我的尝试

我做错了什么

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 ;
}

无法将常规链接列表保存到文件。为了让它工作,您需要完全控制内存分配,包括您获得的地址。一般来说,你不会


如果将链表覆盖在单个缓冲区上,则可以通过将指针转换为索引并返回(或仅使用索引而不是指针)来保存链表,但一般来说,不能将链表保存到磁盘、释放内存(包括退出)、重新加载、,然后期望保存的指针工作。

不应该
rootptr
headptr
?您正在使用从未声明过的变量
root
。您需要将最后一个节点的
下一个
点设置为
NULL
。在开始附加新节点之前,您需要转到列表的末尾。
---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----