Writing and Reading Records to File using Structure

#include<stdio.h>
#include<conio.h>
struct emp
{
  char name[50];
  int age;
  float salary;
};
void main()
{
          FILE *fp;
          int i=1;
          char ans;
          struct emp e;
          clrscr();
          fp=fopen("employee.txt","w");
          if(fp==NULL)
           {
             printf("Error");
             getch();
             exit(0);
           }
          do
          {
                   printf("\n------- Record - %d --------",i);
                   fflush(stdin);
                   printf("\nEnter Employee name : ");
                   gets(e.name);
                    fflush(stdin);
                   printf("\nEnter Employee age  : ");
                   scanf("%d",&e.age);
                   printf("\nEnter salary        : ");
                   scanf("%f",&e.salary);
                   fflush(stdin);
                   fprintf(fp,"%s %d %f",e.name,e.age,e.salary);
                   printf("\n Do you want to continue [y-n] : ");
                   ans=getchar();
                   if(ans=='y' || ans=='Y')
                    {
                             fputs("\n",fp);
                             i++;
                    }
          }while(ans=='y' || ans=='Y');
          fclose(fp);
          fp=fopen("employee.txt","r");
          printf("\n----------------------------------------------------");
          printf("\n Records in File ");
          printf("\n----------------------------------------------------");
          printf("\n  Name\t\t\tAge\t Salary ");
          printf("\n----------------------------------------------------");
          while(fscanf(fp,"%s %d %f",e.name,&e.age,&e.salary)!=EOF)
           {
             printf("\n %-20s  %d  %.2f",e.name,e.age,e.salary);
           }
getch();
}
Program – 10:  Writing and reading block of bytes(fwrite(), fread())
#include<stdio.h>
#include<conio.h>
struct emp
{
  char name[50];
  int age;
  float salary;
};
void main()
{
          FILE *fp;
          int i=1;
          char ans;
       struct emp e;
          clrscr();
          fp=fopen("employee.txt","w");
          if(fp==NULL)
           {
             printf("Error");
             getch();
             exit(0);
           }
          do
          {
                   printf("\n------- Record - %d --------",i);
                   fflush(stdin);
                   printf("\nEnter Employee name : ");
                    gets(e.name);
                   fflush(stdin);
                   printf("\nEnter Employee age  : ");
                   scanf("%d",&e.age);
                   printf("\nEnter salary        : ");
                   scanf("%f",&e.salary);
                   fflush(stdin);
                   //fprintf(fp,"%s %d %f",e.name,e.age,e.salary);
                   fwrite(&e,sizeof(e),1,fp);
                   printf("\n Do you want to continue [y-n] : ");
                   ans=getchar();
                   if(ans=='y' || ans=='Y')
                    {
                         // fputs("\n",fp);
                             i++;
                    }
          }while(ans=='y' || ans=='Y');
          fclose(fp);
          fp=fopen("employee.txt","r");
          printf("\n----------------------------------------------------");
          printf("\n Records in File ");
          printf("\n----------------------------------------------------");
          printf("\n  Name\t\t\tAge\t Salary ");
          printf("\n----------------------------------------------------");
          while((fread(&e,sizeof(e),1,fp))==1)
           {
             printf("\n %-20s  %d  %.2f",e.name,e.age,e.salary);
           }
getch();
}

Post a Comment

Previous Post Next Post