I need some assistance with my code. I've created the function for writing the linked list to a binary file. Now I'm trying to read in a linked list from a binary file that my write function has outputted. My attempt at trying to read in the binary file and creating the link segfaults. This is my attempt below.
What am I doing wrong?
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);
}
I hope this code is helpful to you. if you use a global variable headptr, it will be more simple.
#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 ;
}
the output is this
---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----
You can't save a generic linked list to a file. For it to work, you'd need to be in full control of memory allocations including the addresses you get. Generally, you won't be.
If you overlay the linked list over a single buffer, then you can save it by translating pointers to indices and back (or just by using indices instead of pointers), but generically, you cannot save a linked list to disk, free the memory (including by exiting), reload, and then expect the saved pointers to work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.