1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include <stdio.h>
#include <stdlib.h>
/*Working with structures and binaries files*/
typedef struct person {
char *name = (char *)(malloc (sizeof(char)));
int age;
float height, weight;
} persona;
//int menu (int *choice)
//{
// do{
//
// puts("=========================");
// puts(" MAIN MENU ");
// puts("=========================");
// puts ("1- add structure");
// puts ("2- add ")
//
// } while (choose)
//}
int main (void){
persona pers1, *pers_vet = (persona *)(malloc(sizeof(persona)));
FILE *pnt;
pnt = fopen ("registers.bin", "a+b");
if (pnt == NULL){
printf ("error to open/create the file!!\n");
exit(1);
}
printf ("Insert your name : "); gets (pers1.name);
printf ("Insert your height : "); scanf ("%f", &pers1.height);
printf ("Insert your weight : "); scanf ("%f", &pers1.weight);
printf ("insert yor age : "); scanf ("%d", &pers1.age);
puts (""); puts("");
end_ = fseek (pnt, 0, SEEK_END);
if ((fwrite (&pers1, sizeof (persona), sizeof(pers1), pnt)) < sizeof(pers1)){
printf ("Error writing in the file!!\n");
return 0;
}
if ((fread (&pers_vet[0], sizeof(persona), end_, pnt)) != end_)
printf ("Error reading the file\n");
return 0;
printf ("%s", pers_vet->name);
}
|