Code Competitions

Coding competition

Monday, 13 May 2013

Program to write and read a structure using fwrite and fread functions

#include<stdio.h>
#include<conio.h>
struct stud
{
    char a[20];
    char mbl[20];
    int age;
};
void main()
{

      FILE *fp;
      int i=0;
       char ch;
      struct stud s;
      clrscr();
      fp=fopen("asgh.txt","wb+");
      fflush(fp);
      while(i<n)
      {
      i++;
      printf("enter the name ");
      gets(s.a);
      printf("enter the mobile number  ");
      gets(s.mbl);
      scanf("%d",&(s.age));
      fwrite(&s,sizeof(s),1,fp);
      printf("y for continue and n for done\t");
      fscanf(fp,"%c",&ch);
      fflush(stdin);

      }
      printf("\n\n\n");
      rewind(fp);
      while(!feof(fp))
      {
          fread(&s,sizeof(s),1,fp);
          if(feof(fp))
          break;
          else
          {
             puts(s.a);
            printf("%s %d",s.mbl,s.age);
          }
      }
      getch();
}

No comments:

Post a Comment