c - Copy a list to a binary file -
i'm trying copy contents of struct in list binary file.
here's structures declarations:
typedef struct { char dni[9]; char nombre[100], apellido[100]; double deuda; } t_datos; struct s_nodo { t_datos dati; struct s_nodo *sig; }; typedef struct s_nodo *t_nodo; void newfile(t_nodo, file *); in main fill list data .dat file , , looks ok, because if print list ok.i've got problem when try copy struct i've in list new binary file.
//open , copy old binary file list... fc = fopen("c:\\norep.dat", "w+b"); if (fc == null) { puts("error"); exit(-1); } newfile(lista, fc); here's function:
void newfile(t_nodo lista, file *fc) { if (lista != null) { fwrite(lista->dati, sizeof(t_datos), 1, fc); newfile(lista->sig, fc); } } the error is:
error: incompatible type argument 1 of 'fwrite'|
i know mistake in way i'm passing list function, saw prototype of fwrite don't know how should fix code.
the first argument of fwrite() must pointer (const void * to precise), in case it's not. have use & address of operator, this
fwrite(&lista->dati, sizeof(lista->dati), 1, fc); also, might want check return value of fwrite() sure succeeded, in case since writing single item has 1.
warning: don't typedef poitners, might problem in future. , if must, use kind of notation suffix _ptr indicate it's pointer (libxml-2.0 uses ptr example).
Comments
Post a Comment