C File Handling Programs 19

Project 3 (Product Management System)
(Inventory Control System)

This is a project to maintain product details like product number , product name, rate of product , quantity stored and cost of the product. In this project we have used text file handling to save the product records and perform various operations.

In this project we have use function:
Function used:
fread() and fwrite()
Random function:
ftell();
fseek();

User defined Functions:

void add_product():
In this function, we are taking input for product details like product number, product name, rate of the product, and quantity of product. calculating the total cost of the product and saving the details in the file.

void disp_all_product():
This function helps us to display all the records stored in the file.

void product_search_pno():
In this function, we are taking input for the product number(pno) of the product whose record we want to search and then we perform a sequential search for the record with the mentioned product number(pno), if the record is found it is displayed otherwise “record not found” message is displayed.

void product_search_pname():
In this function we are taking input for the product name of the product whose record we want to search and then we perform a sequential search for the record with mentioned product name, if the records are found they are display otherwise “record not found” message is displayed.

void product_delete_pno():
In this function, we are taking input for the product number(pno) of the product whose record we want to delete and then we perform a sequential search for the record with the mentioned product number(pno), if the record is found it is displayed and deleted, otherwise “record not found” message is displayed.

void product_delete_pname():
In this function, we are taking input for the product name(pname) of the product whose record we want to delete and then we perform a sequential search for the record with the mentioned product name(pname), if the record is found it is displayed and deleted, otherwise “record not found” message is displayed.

void product_modify_pno():
In this function, we are taking input for the product number(pno) of the product whose record we want to modify and then we perform a sequential search for the record with the mentioned product number(pno), if the record is found it is displayed and new value are asked for and saved otherwise “record not found” message is displayed.

void product_modify_pname():
In this function we are taking input for the product name(pname) of the product whose record we want to modify and then we perform a sequential search for the record with mentioned product name(pname), if the record is found it is display and new value are asked for and saved otherwise “record not found” message is displayed.

void product_random():
this function helps us to fetch product record on the basis of position of the record in the database file.


void menu():
This function helps us to display the main menu , where we mention all the operations to be performed.
Operations performed
1. Add a record
2. To display all the records
3. to display a record by product number
4. to display a record by product name
5. delete a record by product number
6. delete a record by product name
7. to modify a record by product number
8. to modify a record by product name
9. To display a particular record

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct product
{
	int pno;
	char name[10];
	float rate,qty,cost;
};
void line()
{
	int i;
	for(i=1;i<=85;printf("="),i++);
	printf("\n");
}

void heading()
{
	int i;
	system("color 0C");
	//system("color 0A");
	printf("\n\n\n\n");
	printf("%55s","Product Management System\n");
	printf("%55s","=========================\n\n");
	printf("%50s","Product Records\n");
	printf("%50s","===============\n");
	line();
	printf("%10s  %20s  %15s %15s %15s\n","product no","product Name","Rate","Quantity","Cost");
	line();
}
void add_product()
{
	FILE *fp;
	struct product p;
	fp=fopen("prod.dat","r");
	if (fp==NULL)
	{
		printf("File is not present , it has to be created\n");
		fp=fopen("prod.dat","w");
	}
	else
	{
		printf("File is present , so open in append mode \n");
		fclose(fp);
		fp=fopen("prod.dat","a");
	}
	printf("\n\n\n\n");
	printf("%55s","Product Management System\n");
	printf("%55s","=========================\n\n");
	printf("%50s","Enter the product number ");
	scanf("%d",&p.pno);
	printf("%50s","Enter the product name");
	scanf("%s",p.name);
	strupr(p.name);
	printf("%50s","Enetr the product rate ");
	scanf("%f",&p.rate);
	printf("%50s","Enter the product qty");
	scanf("%f",&p.qty);
	p.cost=p.rate*p.qty;
	fwrite(&p,sizeof(p),1,fp);
	fclose(fp);
}
void disp_all_products()
{
	FILE *fp;
	struct product p;
	fp=fopen("prod.dat","r");
	heading();
	while((fread(&p,sizeof(p),1,fp)))
	{
		//printf("%10s  %20s  %15s %15s %15s\n","product no","product Name","Rate","Quantity","Cost");
		printf("%10d %20s %15.2f %15.2f %15.2f\n",p.pno,p.name,p.rate,p.qty,p.cost);
	}
	line();
	fclose(fp);
}
void product_search_pno()
{
	FILE *fp;
	struct product p;
	int z,tn;
	z=0;
	printf("\n\n");
	printf("%55s","Product Management System\n");
	printf("%55s","=========================\n\n");
	printf("%50s","Enter the product number ");
	scanf("%d",&tn);
	fp=fopen("prod.dat","r");
	heading();
	while((fread(&p,sizeof(p),1,fp)))
	{
		if (tn==p.pno)
		{
			z=1;
			//printf("%d %s %f %f %f\n",p.pno,p.name,p.rate,p.qty,p.cost);
			printf("%10d %20s %15.2f %15.2f %15.2f\n",p.pno,p.name,p.rate,p.qty,p.cost);
		}
	}
	fclose(fp);
	if (z==0)
		printf("record is not present\n");
	else
		line();
}
void product_search_pname()
{
	FILE *fp;
	struct product p;
	int z;
	char tna[20];
	z=0;
	printf("\n\n");
	printf("%55s","Product Management System\n");
	printf("%55s","=========================\n\n");
	printf("%50s","Enter the product name ");
	scanf("%s",tna);
	strupr(tna);
	fp=fopen("prod.dat","r");
	heading();
	while((fread(&p,sizeof(p),1,fp)))
	{
		if (strcmp(tna,p.name)==0)
		{
			z=1;
			//printf("%d %s %f %f %f\n",p.pno,p.name,p.rate,p.qty,p.cost);
			printf("%10d %20s %15.2f %15.2f %15.2f\n",p.pno,p.name,p.rate,p.qty,p.cost);
		}
	}
	fclose(fp);
	if (z==0)
		printf("record is not present\n");
	else
		line();
	
}
void product_delete_pno()
{
	FILE *fp,*tfp;
	struct product p;
	int z,tn;
	z=0;
	printf("\n\n");
	printf("%55s","Product Management System\n");
	printf("%55s","=========================\n\n");
	printf("%50s","Enter the product number ");
	scanf("%d",&tn);
	fp=fopen("prod.dat","r");
	tfp=fopen("temp","w");
	heading();
	while((fread(&p,sizeof(p),1,fp)))
	{
		if (tn==p.pno)
		{
			z=1;
			//printf("%d %s %f %f %f\n",p.pno,p.name,p.rate,p.qty,p.cost);
			printf("%10d %20s %15.2f %15.2f %15.2f\n",p.pno,p.name,p.rate,p.qty,p.cost);
		}
		else
		fwrite(&p,sizeof(p),1,tfp);
	}
	fclose(fp);
	fclose(tfp);
	if (z==0)
		printf("record is not present\n");
	else
	{
		line();
		remove("prod.dat");
		rename("temp","prod.dat");
	}
}
void product_delete_pname()
{
	FILE *fp,*tfp;
	struct product p;
	int z;
	char tna[20];
	z=0;
	printf("\n\n");
	printf("%55s","Product Management System\n");
	printf("%55s","=========================\n\n");
	printf("%50s","Enter the Name of the product ");
	scanf("%s",tna);
	strupr(tna);
	fp=fopen("prod.dat","r");
	tfp=fopen("temp","w");
	while((fread(&p,sizeof(p),1,fp)))
	{
		if (strcmp(tna,p.name)==0)
		{
			z=1;
			//printf("%d %s %f %f %f\n",p.pno,p.name,p.rate,p.qty,p.cost);
			printf("%10d %20s %15.2f %15.2f %15.2f\n",p.pno,p.name,p.rate,p.qty,p.cost);
		}
		else
			fwrite(&p,sizeof(p),1,tfp);
	}
	fclose(fp);
	fclose(tfp);
	if (z==0)
		printf("record is not present\n");
	else
	{
		line();
		remove("prod.dat");
		rename("temp","prod.dat");
	}
}
void product_modify_pno()
{
	FILE *fp,*tfp;
	struct product p;
	int z,tn;
	z=0;
	printf("\n");
	printf("%55s","Product Management System\n");
	printf("%55s","=========================\n\n");
	printf("%50s","Enter the product number ");
	scanf("%d",&tn);
	fp=fopen("prod.dat","r");
	tfp=fopen("temp","w");
	heading();
	while((fread(&p,sizeof(p),1,fp)))
	{
		if (tn==p.pno)
		{
			z=1;
			//printf("the record to be Modified is found \n");
			//printf("the contents of the record are \n");
			//printf("Pno Name Rate Qty Cost \n");
			//printf("%d %s %f %f %f\n",p.pno,p.name,p.rate,p.qty,p.cost);
			printf("%10d %20s %15.2f %15.2f %15.2f\n",p.pno,p.name,p.rate,p.qty,p.cost);
			printf("\nEnter the new details\n");
			printf("Enter the product number ");
			scanf("%d",&p.pno);
			printf("Enter the product name ");
			scanf("%s",p.name);
			strupr(p.name);
			printf("Enter the product rate ");
			scanf("%f",&p.rate);
			printf("Enter the product qty ");
			scanf("%f",&p.qty);
			p.cost=p.rate*p.qty;
		}
		fwrite(&p,sizeof(p),1,tfp);
	}
	fclose(fp);
	fclose(tfp);
	if (z==0)
		printf("record is not present\n");
	else
	{
		line();
		remove("prod.dat");
		rename("temp","prod.dat");
	}

}
void product_modify_pname()
{
	FILE *fp,*tfp;
	struct product p;
	int z;
	char tna[20];
	z=0;
	printf("\n");
	printf("%55s","Product Management System\n");
	printf("%55s","=========================\n\n");
	printf("%50s","Enter the Name of the product ");
	scanf("%s",tna);
	strupr(tna);
	fp=fopen("prod.dat","r");
	tfp=fopen("temp","w");
	while((fread(&p,sizeof(p),1,fp)))
	{
		if (strcmp(tna,p.name)==0)
		{
			z=1;
			//printf("the record to be Modified is found \n");
			//printf("the contents of the record are \n");
			//printf("Pno Name Rate Qty Cost \n");
			//printf("%d %s %f %f %f\n",p.pno,p.name,p.rate,p.qty,p.cost);
			printf("%10d %20s %15.2f %15.2f %15.2f\n",p.pno,p.name,p.rate,p.qty,p.cost);
			printf("\n\n Enter the new details\n");
			printf("Enter the product number ");
			scanf("%d",&p.pno);
			printf("Enter the product name");
			scanf("%s",p.name);
			strupr(p.name);
			printf("Enetr the product rate ");
			scanf("%f",&p.rate);
			printf("Enter the product qty");
			scanf("%f",&p.qty);
			p.cost=p.rate*p.qty;
		}
		fwrite(&p,sizeof(p),1,tfp);
	}
	fclose(fp);
	fclose(tfp);
	if (z==0)
	printf("record is not present\n");
	else
	{	
		line();
		remove("prod.dat");
		rename("temp","prod.dat");
	}
}
void product_random()
{
	FILE *fp;
	struct product p;
	int n,trec,reclen=0,filelen=0,z=0;
	fp=fopen("prod.dat","r");
	printf("\n");
	printf("%55s","Product Management System\n");
	printf("%55s","=========================\n\n");
	printf("%50s","Enter the number of the record you want to see ");
	scanf("%d",&n);
	reclen=sizeof(p);
	printf("total length of the record is %d\n",reclen);
	/* fseek command helps us to take the file pointer to a
	particular location */
	fseek(fp,0,2);
	filelen=ftell(fp);
	trec=filelen/reclen;
	printf("total length of the file is %d\n",filelen);
	printf("Total number of records are %d\n",trec);
	rewind(fp);
	if (n>0 && n<=trec)
	{
		fseek(fp,sizeof(p)*(n-1),0);
		fread(&p,sizeof(p),1,fp);
		//printf("Product number %d\n",p.pno);
		//printf("Product name %s\n",p.name);
		//printf("Rate %f\n",p.rate);
		//printf("Qty %f\n",p.qty);
		//printf("cost %f\n",p.cost);
		heading();
		printf("%10d %20s %15.2f %15.2f %15.2f\n",p.pno,p.name,p.rate,p.qty,p.cost);
		line();
	}
	else
		printf("Invalid record number is entered \n");
}
void menu()
{
		system("cls");
		system("color 0A");
		printf("\n\n\n\n");
		printf("%55s","Product Management System\n");
		printf("%55s","=========================\n\n");
		printf("%50s","Main Menu \n");
		printf("                              1. Add a record\n");
		printf("                              2. To display all the records \n");
		printf("                              3. to display a record by prod number \n");
		printf("                              4. to display a record by prod name \n");
		printf("                              5. delete a record by  prod number\n");
		printf("                              6. delete a record by prod name \n");
		printf("                              7. to modify a record by prod number \n");
		printf("                              8. to modify a record by prod name \n");
		printf("                              9. To display a particular record \n");
		printf("                              10. Exit \n");
		printf("                              Enter your choice ");
}
int main()
{
	FILE *fp,*tfp;
	struct product p;
	int op=0,n,trec,reclen=0,filelen=0,tn,z=0;
	char tna[10];
	while(op!=10)
	{
		menu();
		scanf("%d",&op);
		system("cls");
		switch(op)
		{
			case 1:
				add_product();
				break;
			case 2:
				disp_all_products();
				break;
			case 3:
				product_search_pno();
				break;
			case 4:
				product_search_pname();
				break;
			case 5:
				product_delete_pno();
				break;
			case 6:
				product_delete_pname();
				break;
			case 7:
				product_modify_pno();
				break;
			case 8:
				product_modify_pname();
				break;
			case 9:
				product_random();
				break;
			case 10:
				printf("End of the program\n");
				break;
			default:
				printf("Invalid option \n");
				break;
		}
	getch();
	}
return(0);
}


Number Of Visitors

0270406