Pro-grammar
Pro-grammar

Reputation: 43

How do I copy the exact same structure I created in a file in the same code?

So I save a somewhat complex structure into a bin file as following:

fwrite(&ca, sizeof(ca),  1, tempor);
for(i=0; i<ca; i++){
    fwrite(&alunue[i].ndp, sizeof(int), 1, tempor);
    for(j=0; j<alunue[i].ndp; j++){
        fwrite(&alunue[i].peract[j].ndu, sizeof(int), 1, tempor);
    }
}
fwrite(alunue, sizeof(alumno), ca, tempor);

"tempor" being the file that in the end replaces the file which I read in "baseda".

The structures look like this:

typedef struct name{
    char n[125];
} name;

typedef struct uda{
    name nomuda;
    name clave;
    float calif;
} uda;

typedef struct periodo{
    int ndu;
    name nomper;
    uda *uniapr;
} periodo;

typedef struct alumno{
    int nua, ndp;
    name n, ap, am;
    periodo *peract;
    struct alumno *a, *s;
} alumno;

and the code I use to read it looks like this:

fread(&n, sizeof(int), 1, baseda);
alunue = malloc(n*sizeof(alumno));
for (i = 0; i < n; i++) {
     fread(&m, sizeof(int), 1, baseda);
     alunue[i].peract = malloc(m*sizeof(periodo));
     for (j = 0; j < m; j++) {
          fread(&o, sizeof(int), 1, baseda);
          alunue[i].peract[j].uniapr = malloc(sizeof(uda));
     }
 }
 fread(alunue, sizeof(alumno), n, baseda);

The thing is, n doesn't seem to be getting the value of ca that I stored in the first place on temp.

Thanks for taking your time reading this.

Upvotes: 0

Views: 53

Answers (1)

user943419
user943419

Reputation: 11

You write using "write()" - try "fwrite()"

Upvotes: 1

Related Questions