C Programming

File Handling – Add, Delete, Modify, Search Records

Program to Add, Delete, Modify, Search records of Employee using a structure in a Binary file using File Handling in C.

Language : C

[sourcecode language='c']
#include
#include
void append();
void list();
void search();
void modify();
void del();

struct employee
{
int no, sal;
char gen, name[20];
};

void main()
{ int a;
char ch;
do{
printf(“\nEMPLOYEE DATABASE\n\n”);
printf(“1.Append Employee Record\n2.List Employee Record\n3.Modify Employee Record\n4.Delete Employee Record\n5.Search Employee Record\n Enter Choice : “);
scanf(“%d”,&a);
switch(a)
{
case 1:
append();
break;
case 2:
list();
break;

case 3:
modify();
break;
case 4:
del();
break;
case 5:
search();
break;
default :
printf(“Invalid Choice!”);
}
printf(“\n More Actions ? (Y/N) :”);
fflush(stdin);
scanf(“%c”, &ch);
}while(ch==’y'|| ch==’Y');
}
void append()
{ int i,n;
struct employee e;
FILE *fp;
fp=fopen(“Employee.dat”, “a”);

if(fp==NULL)
{
printf(“File Creation Failed!”);
exit(0);
}
printf(“Enter the nos. of employees : “);
scanf(“%d”, &n);
for(i=0;i {
printf(“Enter the Employee Number : “);
scanf(“%d”, &e.no);
printf(“Enter the Employee Salary : “);
scanf(“%d”, &e.sal);
printf(“Enter the Employee gender: “);
fflush(stdin);
scanf(“%c”, &e.gen);
printf(“Enter the Employee Name : “);
fflush(stdin);
gets(e.name);
printf(“\n\n”);

fwrite((char *)&e, sizeof(e), 1, fp);
}
fclose(fp);
}

void list()
{ int nofrec=0;
struct employee e;
FILE *fp;
fp=fopen(“Employee.dat”, “rb”);
if(fp==NULL)
{
printf(“\n\tFile doesn’t exist!!!\TRY AGAIN”);
exit(0);
}

while((fread((char *)&e, sizeof(e), 1, fp))==1)
{ nofrec++;
printf(“\nEmployee Number : %d”, e.no);
printf(“\nEmployee Salary : %d”, e.sal);
printf(“\nEmployee gender : %c”,e.gen);
printf(“\nEmployee Name : %s”,e.name);
printf(“\n\n”);
}
printf(“Total number of records present are : %d”, nofrec);

fclose(fp);

}

void modify()
{ int recno, nofrec=0;
char ch;

struct employee e;
FILE *fp;
fp=fopen(“Employee.dat”, “rb+”);

printf(“Enter the Employee Number to modify : “);
scanf(“%d”, &recno);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{ nofrec++;
if(e.no==recno)
{
printf(“\nEmployee Number : %d”, e.no);
printf(“\nEmployee Salary : %d”, e.sal);
printf(“\nEmployee gender : %c”,e.gen);
printf(“\nEmployee Name : %s”,e.name);
printf(“\n”);

printf(“Do you want to modify this record : ? (Y/N)”);
fflush(stdin);
scanf(“%c”, &ch);

fseek(fp, ((nofrec-1)*sizeof(e)), 0);
if(ch==’Y'|| ch==’y')
{
printf(“Enter the Employee Salary : “);
scanf(“%d”, &e.sal);
printf(“Enter the Employee gender: “);
fflush(stdin);
scanf(“%c”, &e.gen);
printf(“Enter the Employee Name : “);
fflush(stdin);
gets(e.name);
fwrite((char *)&e, sizeof(e), 1, fp);
printf(“Record Modified”);
}

else
printf(“No modifications were made”);

fclose(fp);

}
}
}
void del()
{
int recno;
char ch;

struct employee e;
FILE *fp, *ft;
fp=fopen(“Employee.dat”, “rb”);
ft=fopen(“Temp.dat”, “wb”);

printf(“Enter the Employee Number to delete : “);
scanf(“%d”, &recno);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(e.no==recno)
{
printf(“\nEmployee Number : %d”, e.no);
printf(“\nEmployee Salary : %d”, e.sal);
printf(“\nEmployee gender : %c”,e.gen);
printf(“\nEmployee Name : %s”,e.name);
printf(“\n”);

printf(“Do you want to delete this record : ? (Y/N)”);
fflush(stdin);
scanf(“%c”, &ch);

}
}
if(ch==’y'||ch==’Y')
{
rewind(fp);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(recno!=e.no)
{
fwrite((char *)&e, sizeof(e), 1, ft);
}
}
}
else
printf(“No Record was deleted”);

fclose(fp);
fclose(ft);
remove(“Employee.dat”);
rename(“Temp.dat”, “Employee.dat”);

}
void search()
{ int s,recno;
char sname[20];

struct employee e;
FILE *fp;
fp=fopen(“Employee.dat”, “rb”);
printf(“\n1.Search by Name\n2.Search by Employee No.\n Enter choice : “);
scanf(“%d”, &s);
switch(s)
{
case 1:

printf(“Enter the Employee Name to Search : “);
fflush(stdin);
gets(sname);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(strcmp(sname,e.name)==0)
{
printf(“\nEmployee Number : %d”, e.no);
printf(“\nEmployee Salary : %d”, e.sal);
printf(“\nEmployee gender : %c”,e.gen);
printf(“\nEmployee Name : %s”,e.name);
printf(“\n”);

}
}
break;

case 2:

printf(“Enter the Employee Number to Search : “);
scanf(“%d”, &recno);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(e.no==recno)
{
printf(“\nEmployee Number : %d”, e.no);
printf(“\nEmployee Salary : %d”, e.sal);
printf(“\nEmployee gender : %c”,e.gen);
printf(“\nEmployee Name : %s”,e.name);
printf(“\n”);

}
}
break;
}
}
[/sourcecode]



{ 7 comments… read them below or add one }

1 NAVNEET SWAMI May 11, 2010 at 4:06 am

I am taking more and more knows of C from this Web site and it’s very help full for many student for doing his program’s of C.

Reply

2 Programming Kid May 14, 2010 at 5:41 pm

Hi Navneet

Welcome to the community! I am glad you like ProgrammingKid.

If you have any programs or contributions, you wish to make. Do write in!

Vaibhav

Reply

3 NAVNEET SWAMI May 11, 2010 at 6:06 am

I am taking more and more knows of C from this Web site and it’s very help full for many student for doing his program’s of C.

Reply

4 Programming Kid May 14, 2010 at 7:41 pm

Hi Navneet

Welcome to the community! I am glad you like ProgrammingKid.

If you have any programs or contributions, you wish to make. Do write in!

Vaibhav

Reply

5 lenda November 19, 2010 at 2:07 pm

plzzzzzzzzzzzzzzzzzzzzz i wana this code
in visual c++ 2008
plzzzzzzzzzzzzzz
i will appreciate that

Reply

6 Rohan November 23, 2010 at 6:30 am

hey ur source kid helped me alot in understanding c++ file handling…
i need ur further help…cn u pls help me out in visual basic…i dnt noe netin abt dat lang…
pls help lyk dis…
thanks

Reply

7 dhang January 9, 2011 at 11:39 pm

codes worked… it helped me a lot. thanks to this site. keep up!

Reply

Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.


WordPress - Vaibhav Kanwal