Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving and loading a tree using fwrite / fread
    primarykey
    data
    text
    <p>I'm a noob in C and I do have to save and load a binary tree(red black tree) that has structs as nodes, each node is RBdata: </p> <pre><code>typedef struct RBdata_ { // The variable used to index the tree has to be called "key". TYPEKEY key; //key is of type char* int numFitxers; int *fileCount; } RBdata; </code></pre> <p>and each node is as follows:</p> <pre><code>typedef struct Node_ { /* For internal use of the structure. Do not change. */ struct Node_ *left; /* left child */ struct Node_ *right; /* right child */ struct Node_ *parent; /* parent */ nodeColor color; /* node color (BLACK, RED) */ /* Data to be stored at each node */ RBdata *data; /* data stored in node */ } Node; </code></pre> <p>my problem is that I don't know where my error is, in the save function or in the load one:</p> <pre><code>RBTree loadTree(char *name){ RBTree tree; initTree(&amp;tree); RBdata *data; FILE *fp; if((fp=fopen(name,"r"))!=NULL){ data=malloc(sizeof(RBdata)); while(fread(&amp;data-&gt;key,sizeof(char),MAXCHAR,fp)==1){ fread(&amp;data-&gt;numFitxers,sizeof(int),1,fp); data-&gt;fileCount=malloc(sizeof(int)*595); fread(&amp;data-&gt;fileCount,sizeof(int),595,fp); insertNode(&amp;tree,data); data=malloc(sizeof(RBdata)); } fclose(fp); } else{ printf("Error: Cannot read the file.\n"); } return tree; } void saveTree(Node *a, FILE * file1) { if (a-&gt;right != NIL) saveTree(a-&gt;right,file1); if (a-&gt;left != NIL) saveTree(a-&gt;left,file1); fwrite(a-&gt;data-&gt;key, sizeof(char),MAXCHAR,file1); //key, numFiles i fileCount fwrite(&amp;a-&gt;data-&gt;numFitxers, sizeof(int),1,file1); fwrite(a-&gt;data-&gt;fileCount, sizeof(int),595,file1); } </code></pre> <p>When trying to load a tree I get a "segmentation fault". Could you give me some help? Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload