Thursday, April 4, 2013

WAP Structure with File Handling


#include<stdio.h>
#include<conio.h>
struct product
{
int c;
char name[20];
int qty,cost;
}p[100];


void main()
{
int i,n;
FILE *p1;
clrscr();
printf("\n\nhowmany product in shop.....");
scanf("%d",&n);
printf("enter details\n");
p1=fopen("F:\\c\\sam2\\product.txt","w+");
for(i=0;i<n;i++)
{
fflush(stdin);
printf("enter product code.....\n");
scanf("%d",&p[i].c);
fflush(stdin);
printf("enter name of product.....\n");
gets(p[i].name);
fflush(stdin);
printf("enter quntity of product.....\n");
scanf("%d",&p[i].qty);
printf("enter cost of product.......\n");
scanf("%d",&p[i].cost);
fflush(stdin);

fprintf(p,"%d\t%s\t%d\t%d\n",p[i].c,p[i].name,p[i].qty,p[i].cost);
}
printf("\nprint all details\n");
rewind(p);
for(i=0;i<n;i++)
{
fscanf(p,"%d\t%s\t%d\t%d\n",&p[i].c,p[i].name,&p[i].qty,&p[i].cost);
printf("Dtail of product=%d\tname=%s\tcode=%d\tqntity=%d\tcost=%d\n",i+1,p[i].name,p[i].c,p[i].qty,p[i].cost);
}
getch();
}

No comments:

Post a Comment