¿Cómo utilizar la función fread para leer archivos de disco en una lista vinculada?
#include
#include
#include
#define szSTR 32
struct TBook {
int número ; /*Número de código del libro*/
char título[szSTR] ;/*Nombre del libro* /
char autor[szSTR]; /*Autor*/
int año;/*Tiempo de publicación*/
precio flotante; /
int almacenamiento; /*inventario*/
int export;
struct TLink {
struct TBook book; >
estructura TLink * siguiente
};
estructura TLink * cargar(char * archivo)
{
ARCHIVO * f = 0; struct TBook libro; int r = 0;
struct TLink * n = 0, * datos = 0;
if(!file || !data) return 0;
f = fopen(file, "rb");
if(!f) devuelve 0;
while(!feof(f)) {
r = fread(&book, sizeof(book), 1, f);
if (r < 1) break;
n = (struct TLink *)malloc (tamaño de(*n));
memset(n, 0, tamaño de(*n ));
memcpy(n, &libro, tamaño de(libro));
n->siguiente = datos;
datos = n;
}
fclose(f);
regresar datos;
}
void Guardar(char * archivo, estructura TLink * datos)
{
ARCHIVO * f = 0; estructura TLink * n = 0;
if(! archivo ||| !datos) retorno
f = fopen( archivo, "wb");
if(!f) retorno
n = datos;
while(n) {
fwrite(n, sizeof(struct TBook), 1, f);
n = n->siguiente;
}
fclose(f);
}
int main(void)
{ p>
estructura TLink * libros = 0;
libros = Cargar("libros.txt");
Guardar("libros. bak", libros);
devolver 0;
}