How2Lab Logo
tech guide & how tos..


File Handling in C - Part 7 of 7


Project Work

This is a project work for you. Part of the program is written by me. You have to write the codes for functions modifyRecord() & deleteRecord().

/*
 * Employee Record Keeping & Management Program
 * --------------------------------------------
 */

 #include <stdio.h> 

 typedef struct{
   char  fname[21];
   char  lname[21];
   char  joinDt[11]; //dd/mm/yyyy
   char  dept[51];
   char  empId[11];
   int   age;
   float sal;
 } Employee;

 Employee *emp;

 void addRecord(void)
 {
   FILE *fp;
   char another='Y';

   fp = fopen("emp.dbf", "ab");
   if(fp==NULL)
   {
	printf("\n\nERROR! Unable to open Employee Database\n\n");
	exit();
   }

   emp = (Employee *)malloc(sizeof(Employee));
   while(another=='Y'|| another=='y')
   {
	printf("\n\nEmployee Name (First Name, Last Name separated by space) : ");
	scanf("%s %s",emp->fname, emp->lname); getchar(); fflush(stdin);

	printf("\nAge : ");
	scanf("%d", &emp->age); getchar(); fflush(stdin);

	printf("\nDepartment : ");
	gets(emp->dept);

	printf("\nJoining Date (dd/mm/yyyy) : ");
	scanf("%s", emp->joinDt); getchar(); fflush(stdin);

	printf("\nEmployee Id : ");
	scanf("%d", &emp->empId); getchar(); fflush(stdin);

	printf("\nGross Salary : ");
	scanf("%f", &emp->sal); getchar(); fflush(stdin);

	fwrite(emp, sizeof(Employee), 1, fp);

	printf("\nContinue to add more records (Y/N) ? ");
	another = getchar(); fflush(stdin);
   }
   fclose(fp);
   return;
 }

 void modifyRecord(void)
 {
   /*
     Write this Code
   */
 }

 void deleteRecord(void)
 {
   /*
     Write this Code
   */
 }

 void listRecords(void)
 {
   FILE *fp;
   fp = fopen("emp.dbf", "rb");
   if(fp==NULL)
   {
	printf("\n\nERROR! Unable to open Employee Database\n\n");
	exit();
   }
   emp = (Employee *)malloc(sizeof(Employee));

   printf("\n\nList of Employee Records:\n");
   printf("----------------------------\n");
   do{
      fread(emp, sizeof(Employee), 1, fp);
      if(feof(fp)) break;

      printf("\n\n%s %s\n", emp->fname, emp->lname);
      printf("Id: %d\n", emp->empId);
      printf("Age: %d\n", emp->age);
      printf("Department: %s\n", emp->dept);
      printf("Joining Date: %s\n", emp->joinDt);
      printf("Gross Salary: %.2f\n", emp->sal);
   }while(1);

   fclose(fp);
   return;
 }

 main()
 {
   FILE *fp;
   char choice;

   //Check if employee database exists. If not, create an empty file
   fp = fopen("emp.dbf", "r");
   if(fp==NULL)
   {
	fp = fopen("emp.dbf", "w");
	if(fp==NULL)
	{
	  printf("\n\nERROR! Unable to create Employee Database\n\n");
	  exit();
	}
   }
   fclose(fp);

   while(1)
   {
	printf("\n\n");
	printf("  1. Add Records \n");
	printf("  2. Delete Records \n");
	printf("  3. Modify Records \n");
	printf("  4. List Records \n");
	printf("  0. Exit \n\n");
	printf("Enter your choice : ");
	scanf("%c",&choice); fflush(stdin);
	printf("\n\n");

	switch(choice)
	{
	  case '1': addRecord();    break;
	  case '2': modifyRecord(); break;
	  case '3': deleteRecord(); break;
	  case '4': listRecords();  break;
	  case '0': exit();         break;
	}
   }
 }



Share:
Buy Domain & Hosting from a trusted company
Web Services Worldwide | Hostinger
About the Author
Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of premier engineering colleges & management institutes, and IT professionals.

Rajeev has founded Computer Solutions & Web Services Worldwide. He has hands-on experience of building variety of websites and business applications, that include - SaaS based erp & e-commerce systems, and cloud deployed operations management software for health-care, manufacturing and other industries.


Refer a friendSitemapDisclaimerPrivacy
Copyright © How2Lab.com. All rights reserved.