#include<conio.h>
#include<stdio.h>
#include<string.h>
#define nomar "esperanza.dat"
typedef struct persona
{ char nombre[10],apellido[10],edad[3];
};
void imprimir();
void agregar();
void menu();
FILE *f;
persona p,p1;
char k;
int nr;
char nom[10],ape[10],ed[3];
void main()
{ while (k!='3')
{ menu();
if (k=='1')
agregar();
if (k=='2')
imprimir();
}
}
void agregar()
{ int i;
for (i=0;i<10;i++)
{ nom[i]=0;
ape[i]=0;
}
clrscr();
gotoxy(2,2);printf("Nombre: ");
//scanf("%10s", &nom);
gets(nom);
gotoxy(2,4);printf("Apellido: ");
//scanf("%10s", &ape);
gets(ape);
gotoxy(2,6);printf("Edad: ");
scanf("%3s", &ed);
if ((f=fopen(nomar,"rb"))==NULL)
{ f=fopen(nomar,"wb");
}
else
{ f=fopen(nomar,"a+b");
}
strcpy(p.nombre,nom);
strcpy(p.apellido,ape);
strcpy(p.edad,ed);
fwrite(&p, sizeof(p), 1, f);
fclose(f);
}
void imprimir()
{ clrscr();
f=fopen(nomar,"rb");
while (fread(&p1, sizeof(p1), 1, f)!=NULL)
{ printf("\n%20s %20s %20s",p1.nombre,p1.apellido,p1.edad);
}
fclose(f);
getch();
}
void menu()
{ clrscr();
gotoxy(2,2);printf("MENU PRINICPAL");
gotoxy(4,4);printf("[1] - AGREGAR");
gotoxy(4,5);printf("[2] - IMPRIMIR");
gotoxy(4,6);printf("[3] - SALIR");
gotoxy(2,8);printf("Elija: ");
k='0';
while (!(k>='1' && k<='3'))
{ k=getch();
}
}