Untitled
unknown
c_cpp
3 years ago
1.8 kB
6
Indexable
#include <stdio.h>
#include <stdlib.h>
#define FILE_NAME "data.bin"
typedef struct { // this structure is the one im using to store the data to
// write in the file
int cedula;
char Nombre[150];
char cumpleanos[60];
char correo[150];
char categoria[30];
} colector;
// function for the reading of the info in the file
void leerDocumento() {
FILE *archivo = fopen(FILE_NAME, "r");
colector colec;
if (archivo == NULL) { exit(1); }
printf("Leyendo: ");
fread(&colec, sizeof(colector), 1, archivo);
// while(fread(&colec, sizeof(colecc), 1, archivo))
while (!feof(archivo)) {
printf(
"Cedula: %d\nNombre: %s\nCategoria: %s\nCumpleanos: %s\nCorreo: %s\n",
colec.cedula,
colec.Nombre,
colec.categoria,
colec.cumpleanos,
colec.correo);
fread(&colec, sizeof(colector), 1, archivo);
}
printf("endDoc\n");
fclose(archivo);
}
// this function asks for the data and stores it in the file, also creates a
// node in the linked list at the end of the function but that works perfectly
void insertarColeccionista(void) {
FILE *archivo = fopen(FILE_NAME, "a");
if (archivo == NULL) exit(1);
colector col;
printf("Ingrese el numero de cedula:\n");
scanf("%d", &col.cedula);
printf("\nIngrese la categoria, bronce, plata, oro o zafiro:\n");
scanf("%s", col.categoria);
printf("\nIngrese el nombre del coleccionista:\n");
scanf("%s", col.Nombre);
printf("Ingrese el cumpleaños del coleccionista:\n");
scanf("%s", col.cumpleanos);
printf("Ingrese el correo del coleccionista:\n");
scanf("%s", col.correo);
fwrite(&col, sizeof(colector), 1, archivo);
fclose(archivo);
leerDocumento();
}
int main(void) {
insertarColeccionista();
}Editor is loading...