#include
#include
#include
struct product
{
int id;
char name;
int price;
int qty;
};
struct Bill
{
int pid;
char pname;
int pprice;
};
char mygetch();
int getid();
int billfileno();
void manageproduct();
void purchaseproduct();
void generatebill();
void addproduct();
void displayallproducts();
struct product findproduct(int id);
void updateproduct(int id, int qty);
char fproduct[]={\"product.txt\"};
char fbill[]={\"bill.txt\"};
int total=0;
int main()
{
File *fp;
int ch;
while(1)
{
printf(\"welcome product management system\ \");
printf(\"manage product\ \");
printf(\"\\purchase product\ \");
printf(\"generate bill\ \");
printf(\"exit\ \");
printf(\"enter your choice\");
scanf(\"%d\",&ch);
switch(ch)
{
case1:manageproduct();
break;
case2:purchaseproduct();
break;
case3:generate bill();
break;
case4:exit();
}
mygetch();
}
return 0;
int getid()
{
file *fp;
int value=0;
fp=fopen(\"product.txt\",\"r\");
if(fp==null)
{
fp=fopen(\"product.txt\",\"w\");
fprint(fp,\"%d\",0);
fclose(fp);
fp=fopen(\"product.txt\",\"r\");
}
fscanf(fp,\"%d\",&value);
fclose(fp);
fp=fopen(\"product.txt\",\"w\");
fprintf(\"fp,\"%d\",value+1);
fclose(fp);return value+1;
void manageproduct()
{
int ch
init back=0;
while(1)
{
printf(\"welcome product management system\ \");
printf(\"1.add new product\ \");
printf(\"2.display all products\ \");
printf(\"back\ \");
printf(\"enter your choice\ \");
scanf(\"%d\",&ch);
switch(ch)
{
case1:addproduct();
break;
case2:displayallproduct();
break;
case3:(back==1)
break;
else
{
mygetch();
}
}
}
void addproduct()
{
file *fp;
struct product t1;
fp=fopen(fproduct,\"ab\");
t1.id=getid();
printf(\"enter product name\");
scanf(\"%s\",&t1.name);
printf(\"enter product price\");
scanf(\"%d\",&t1.price);
printf(\"enter product qty\");
scanf(\"%d\",&t1.qty);
fwrite(&t1,sizeof(t1),1,fp);
fclose(fp);
void dis[playallproducts()
{
file *fp;
struct product t;
int id,found=0;
fp=fopen(fproduct,\"rb\");
printf(\"product details\ \");
printf(\"id\\t name \\tqty\\tprice\ \ \");
while(1)
{
fread(&t,sizeof(t),1,fp);
if(feof(fp))
{
break;
}
printf(\"%d\\t\",t.id);
printf(\"%s\\t\",t.name);
printf(\"%d\\t\",t.qty);
printf(\"%d\\t\",t.price);
fclose(fp);
}
void purchase product()
char ch1,ch2;
int id;
while(1)
{
displayallproducts();
_fpurge(stdin);
printf(\"do u want to purchase y/n\");
scanf(\"%c\",&ch1);
if(ch1==\'y\')
{
file *fp;
struct bill t1;
struct product t2;
fp=fopen(fbill,\"ab\");
printf(\"enter the product id to purchase\");
scanf(\"%d\",&id);
t2=findproduct(id);
t1.pid=t2.id;
strcpy(t1.pname,t2.name);
t1.pprice=t2.price;
fwrite(&t1,sizeof(t1),1,fp);
fclose(fp);
}
_fpurge(stdin);
printf(\"do u want to continue y/n\");
scanf(\"%c\",&ch2);
if(ch2==\'n\')
{
break;
}
mygetch();
}
}
struct product findproduct(int id)
{
file *fp;
struct product t;
fp=fopen(fproduct,\"rb\");
if(feof(fp))
{
break;
}
if(t.id==id)
{
updateproduct(id,t.qty-1);
break;
}
}
fclose(fp);
return t;
}
void updateproduct(int id,int qty)
{
file *fp,*fp1;
struct product t,t1;
int found=.