Pages

Friday, 9 February 2018


In a small firm employee numbers are given in serial numerical order, that is 1, 2, 3, etc.
− Create a file of employee data with following information: employee number, name, sex, gross salary.
− If more employees join, append their data to the file.
− If an employee with serial number 25 (say) leaves, delete the record by making gross salary 0.
− If some employee’s gross salary increases, retrieve the record and update the salary.
Write a program to implement the above operations.

Solution:

#include<stdio.h>
#include<conio.h>
void main() {

struct emp {
    int empno;
    char name[30];
    char sex;
    float gs;
    } e;
FILE *fp,*ft;
int long recsize;
int empn,flag=0;
float new_sal;
char another,ch;
clrscr();

recsize=sizeof(e);

fp=fopen("EMP.DAT","rb+");

if(fp==NULL) {

fp=fopen("EMP.DAT","wb+");
if(fp==NULL)

exit();

}

while(1) {

clrscr();

printf("\t\tEmployee database management\n");
printf("\t\t****************************\n");
printf("\n\n\t1: Add another employee: ");
printf("\n\n\t2: Add salary information of employee: ");
printf("\n\n\t3: List all records: ");
printf("\n\n\t4: Delete employee with 0 salary: ");
printf("\n\n\t0: Exit:");

gotoxy(2,24);
printf("your choice: ");
fflush(stdin);
ch=getche();

switch(ch) {

case '1':

clrscr();

fseek(fp,0,SEEK_END);  /* seeking cursor to reach at the end of file */

another='Y';

while(another=='Y' || another=='y') {

printf("\n\tEnter new employee information\n");
printf("\t******************************\n\n\n");

printf("\nEnter employee number: ");
scanf("%d",&e.empno);

printf("\n\nEnter employee name: ");
scanf("%s",e.name);

printf("\n\nEnter sex(M/F/O): ");
scanf(" %c",&e.sex);

printf("\n\nEnter gross salary: ");
scanf("%f",&e.gs);

/* writing new information at the end of file */

fwrite(&e,recsize,1,fp);

printf("\n\n\n\nAdd another employee(Y/N): ");
fflush(stdin);
another=getche();

clrscr();

}

break;

case '2':

clrscr();

another='Y';

while(another=='Y' || another=='y') {

printf("\n\tEnter salary information\n");
printf("\t************************\n\n");


gotoxy(2,23);    /* showing message at the bottom of the screen */

printf("NOTE: to delete an employee, mark his/her salary 0\n");
printf("       then use option 4 from main menu.");

gotoxy(3,5);     /* returning cursor back from the bottom */
printf("Enter employee number: ");
scanf("%d",&empn);  /* asking for employee number to search */

rewind(fp);

while(fread(&e,recsize,1,fp)==1) {

if(e.empno-empn==0) {    /* if employee number matches with structure */

flag=1;              /* condition indicator for printing further messages */

printf("\n\nEnter new salary for employee:  ");

scanf("%f",&e.gs);

e.empno=e.empno;            /* rest information should be same except only\
    salary */
e.sex=e.sex;

e.name[30]=e.name[30];

fseek(fp,-recsize,SEEK_CUR);  /* seeking the correct location of data within\
    structure in the file */
fwrite(&e,recsize,1,fp);    /* writing data at correct position */

break;

}
}

if(flag==0)       /* conditional indicator used above */
printf("\n\n\n\tinformation does not exist!\n");


printf("\n\nenter another information(Y/N): ");
another=getche();

clrscr();

}

break;

case '4':

clrscr();

printf("\n\n\tDelete employee\n");
printf("\t***************\n\n");

ft=fopen("TEMP.DAT","w");    /* opening new temporary file */

rewind(fp);     /* taking cursor back to the very beginning of file */

while(fread(&e,recsize,1,fp)==1)  { /* matching each salary  */


if(e.gs!=0.0 || e.gs!=0) {      /* if salary is not 0 then data will be written to new
       file */

flag=1;

fwrite(&e,recsize,1,ft);

}
}
fclose(fp);
fclose(ft);

remove("EMP.DAT");      /* removing original file with 0 salary and renaming\
      temporary without 0 salary as the original file */

rename("TEMP.DAT","EMP.DAT");

fp=fopen("EMP.DAT","rt+");  /* opening the new file, it opens because it has
          not been opened before */
       /* a file cannot be opened twice during
          execution as you know */

if(flag>0) {
printf("\n\n\nall records with 0 gross salary have been deleted. \n");
}


gotoxy(2,24);
printf("\n\n\npress any key to return...");
getch();

break;

case '0':

fclose(fp);
exit();

case '3':

clrscr();

printf("\t\tList all employees\n");
printf("\t\t******************\n\n\n");

rewind(fp);

while(fread(&e,recsize,1,fp)==1)  {

flag=1;

printf("%2d\t%6s\t%c\t%.2f\n\n",e.empno,e.name,e.sex,e.gs);

}

if(flag==0)

printf("\n\n\tNo records exist!\n\n");

printf("\n\npress any key to return... ");

getch();    /* this is very important place, if we don't stop screen here \
        after reading and printing the list,we won't be able to see it
        and it will disappear and will return to main menu because
        of "break" statement. */
break;

    }
  }


}
linkfloat() {  /* function to avoid possible errors because of floats */
float a=0,*b;
b=&a;
a=*b;
return 0;
}