¤ Home » Programming » C Tutorial » File Handling in C - Part 7 of 7
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; } } }
File Handling in C - Part 1 of 7
File Handling in C - Part 2 of 7
File Handling in C - Part 3 of 7
File Handling in C - Part 4 of 7
How to move your Email accounts from one hosting provider to another without losing any mails?
How to resolve the issue of receiving same email message multiple times when using Outlook?
Self Referential Data Structure in C - create a singly linked list
Mosquito Demystified - interesting facts about mosquitoes
Elements of the C Language - Identifiers, Keywords, Data types and Data objects
Moving Email accounts from one cPanel server to another
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 XLRI, industry professionals, and govt. officials.
Rajeev has founded Computer Solutions & WebServicesWorldwide.com, and has hands-on experience of building variety of web applications and portals, that include - SAAS based ERP & e-commerce systems, independent B2B, B2C, Matrimonial & Job portals, and many more.
Copyright © How2Lab.com. All rights reserved.