Code Competitions

Coding competition

Monday, 13 May 2013

Program to enter inventry detail in a file using fprintf and read using fscanf

#include<stdio.h>
#include<conio.h>
void main()
{
    FILE *fp;
    int number, quantity, i;
    float price, value;
    char item[10], filename[10];
    clrscr();
    printf("\n\n Input file name: ");
    scanf("%s",filename);
    fp=fopen(filename,"w");
    printf("\n\n Input inventory data: \n");
    printf("\n Item Name   Number  Price  Quantity\n");
    for(i=1;i<=3;i++)
    {
        //scanf("%s %d %f %d",item,&number,&price,&quantity);
        fscanf(stdin,"%s %d %f %d",item,&number,&price,&quantity);
        fprintf(fp,"%s %d %2f %d",item,number,price,quantity);
    }
    fclose(fp);
    fprintf(stdout,"\n\n");
    fp=fopen(filename,"r");
    printf("Item name  Number Price Quantity Value\n");
    for(i=1;i<=3;i++)
    {
        fscanf(fp,"%s %d %f %d",item,&number,&price,&quantity);
        value=price * quantity;
        fprintf(stdout,"%s %d %f %d",item,number,price,quantity,value);

    }
    fclose(fp);
    getch();
}

No comments:

Post a Comment