I'll be back with another question because I've been doing it all day, I want to store the struct
data in a file, but I get an error. Before putting the code I explain that I have the variable contador
, which is where I store every time I add a item
.
When I save struct
I save it in TXT
to be able to visualize the data without coding, but random values and symbols come out. On the other hand, when loading it does not load anything, it detects that there is some data, but it does not detect the data as such, why? When I'm going to delete a copy or modify it, it does not indicate that it's empty BDD
.
Save:
void Guardar(tRegLista *reg)
{
FILE *pfichero;
int i;
pfichero=fopen("BIBLIOTECA.DAT","w+b");
if(pfichero!=NULL){
fwrite(reg->contador,sizeof(reg->contador),1,pfichero);
for(i=0;i<reg->contador;i++){
fwrite(®[i],sizeof(reg[i]),1,pfichero);
}
}
fclose(pfichero);
}
Upload:
void Cargar(tRegLista *reg)
{
FILE *pfichero;
pfichero=fopen("BIBLIOTECA.DAT","rb");
if(pfichero!=NULL){
fread(®->contador,sizeof(reg->contador),1,pfichero);
while(!feof(pfichero)){
fread(®[reg->contador],sizeof(reg[reg->contador]),1,pfichero);
reg->contador++;
}
reg->contador--;
}
fclose(pfichero);
}
Struct:
typedef struct
{
char nomEditorial[TAM];
int numPagina;
} tRegLibros;
typedef struct
{
char nomRevista[TAM];
int numRevista;
int paginaInicio;
} tRegArticulos;
typedef struct
{
char titulo[50];
char autor[TAM];
char idEjemplar[5];
int anioPubli;
int prestado;
char fecha[20];
char dniSocio[9];
char clave[4];
union
uTipo
{
tRegArticulos articulo;
tRegLibros libro;
} uTipo;
} tRegEjemplares;
typedef struct
{
tRegEjemplares ejemplar[maxEJEMPLARES];
int contador;
int contadorLibros;
int contadorArticulo;
} tRegLista;
Main:
tRegLista reg;