Writing and Reading Mixed data (fprintf() , fscanf()) as user Desire

#include<stdio.h>
#include<conio.h>
void main()
{
          FILE *fp;
          int icode,iqty,i=1;
          double iprice,iamt;
          char *iname,ans;
          clrscr();
          fp=fopen("itemdata1.txt","w");
          printf("\n ========Enter Data=======");
          do
          {
                   //iamt=0.0;
                   printf("\n------- Record - %d --------",i);
                   printf("\nEnter Item code : ");
                   scanf("%d",&icode);
                   fflush(stdin);
                   printf("\nEnter Item name : ");
                   gets(iname);
                   printf("\nEnter Price     : ");
                   scanf("%lf",&iprice);
                   printf("\nEnter Quantities : ");
                   scanf("%d",&iqty);
                   iamt=iprice*iqty;
                   fflush(stdin);
                   fprintf(fp,"%d %lf %d %lf %s",icode,iprice,iqty,iamt,iname);
                   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("itemdata1.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----------------------------------------------------");
          while(!feof(fp))
           {
                   fscanf(fp,"%d %lf %d %lf %s",&icode,&iprice,&iqty,&iamt,iname);
                   printf("\n %4d %-20s  %lf  %d  %lf",icode,iname,iprice,iqty,iamt);
           }
getch();
}

Post a Comment

Previous Post Next Post