/* program to read & write structure data from existing file
using fread() & fwrite() functions */
#include<stdio.h>
#include<conio.h>
struct emp
{char name[20];
int id;
}e;
void main()
{ char ch='y';
FILE *pk=fopen("keviv.txt","aw+");
clrscr();
if(pk==NULL)
{ printf("\n\n FILE DOES NOT EXIST");
getch();
exit(0);
}
while(ch=='y')
{ printf("\n\n Enter records of employee:\n");
printf("\n Enter name: ");
scanf("%s",e.name);
printf("\n Enter ID: ");
scanf("%d",&e.id);
fwrite(&e,sizeof(e),1,pk);
printf("\n\n Do u want to continue... ");
fflush(stdin);
scanf("%c",&ch);
}
fclose(pk);
pk=fopen("keviv.txt","ar+");
while(fread(&e,sizeof(e),1,pk)!=EOF)
{ printf(" %s %d\n",e.name,e.id);
break;
}
fclose(pk);
getch();
}
Comments
Post a Comment