0

Im trying to read a binary file (movies.dat) which is a file of linked structs. im trying to read it with fread but it throws segmentation fault.

this is my struct:

struct list_node{
  int number;
  char *name;
  int year;
  float money;
  struct list_node *next;
};
typedef struct list_node node;
node *root;

i have open it with fopen

fp=fopen("movies.dat","rb+");
if(fp==NULL){
exit(1);
}

but this didnt work probably. How can I open and read the binary file, with fread?

  • 2
    A simple `fread()` call isn't going to initialize your linked list, sounds to me like you need a lot more work. Each node must have memory allocated and its contents read into it, the next node would be read and appended to the previously read node...etc. – Chol Nhial Jan 12 '18 at 11:08
  • *a binary file (movies.dat) which is a file of linked structs* is really weird. And even if it was that, pointers in files can only be offsets inside the file meaning that you will have to convert them. I assume that `struct list_node` is the way you want to process it in memory, so please show the actual structure of the *file*. – Serge Ballesta Jan 12 '18 at 11:17
  • 2
    There are many duplicates of this. You cannot write a pointer value to a file (well you can, but it's not very useful.) You need to write the information that the pointer points to, not the pointer value. This process is called "serialization" (google that). Also google "serialize linked list c" and have a look at this [SO question](https://stackoverflow.com/questions/6002528/c-serialization-techniques) – Jabberwocky Jan 12 '18 at 11:17

0 Answers0