Writing and Reading Mixed Data (fprintf() , fscanf())

#include<stdio.h>
#include<conio.h>
void main()
{
          FILE *fp;
          int icode,iqty,i;
          double iprice,iamt;
          char *iname;
          clrscr();
          fp=fopen("itemdata.txt","w");
          printf("\n ========Enter Data=======");
          for(i=0;i<2;i++)
          {
                   printf("\n------- Record - %d --------",i+1);
                   printf("\nEnter Item code : ");
                   scanf("%d",&icode);
                   fflush(stdin);
                   printf("\nEnter Item name : ");
                   gets(iname);
                   fflush(stdin);
                   printf("\nEnter Price     : ");
                   scanf("%lf",&iprice);
                   fflush(stdin);
                   printf("\nEnter Quantities : ");
                   scanf("%d",&iqty);
                   fflush(stdin);
                   iamt=iprice*iqty;
                   fprintf(fp,"%d %s %lf %d %lf",icode,iname,iprice,iqty,iamt);
                   fputs("\n",fp);
          }
          fclose(fp);
          fp=fopen("itemdata.txt","r");
          printf("\n----------------------------------------------------");
          printf("\n Records in File ");
          printf("\n----------------------------------------------------");
          printf("\n  Code\t  Name\t\t\tPrice\t Quantity\t Amount ");
          printf("\n----------------------------------------------------");
          for(i=0;i<2;i++)
           {
                   fscanf(fp,"%d %s %lf %d %lf",&icode,iname,&iprice,&iqty,&iamt);
                   printf("\n %d %s  %lf  %d  %lf",icode,iname,iprice,iqty,iamt);
           }
getch();
}

Post a Comment

Previous Post Next Post