Wednesday 18 March 2015

Program to maintain employee details using files in C


# include <stdio.h>
# include <conio.h>
void main()
{
FILE *fptr ;
int i, n, empno ;
float bpay, allow, ded ;
char name[10] ;
clrscr() ;
fptr = fopen("EMPLOYEE.DAT", "w") ;
printf("Enter the number of employees : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the employee number : ") ;
scanf("%d", &empno) ;
printf("\nEnter the name : ") ;
scanf("%s", name) ;
printf("\nEnter the basic pay, allowances & deductions : ") ;
scanf("%f %f %f", &bpay, &allow, &ded) ;
fprintf(fptr, "%d %s %f %f %f \n", empno,name,bpay,allow,ded);
}
fclose(fptr) ;
fptr = fopen("EMPLOYEE.DAT", "r") ;
printf("\nEmp. No.Name\t\t Bpay\t\t Allow\t\t Ded\t\t Npay\n\n");
for(i = 0 ; i < n ; i++)
{
fscanf(fptr,"%d%s%f%f%f\n", &empno,name,&bpay,&allow,&ded);
printf("%d \t %s \t %.2f \t %.2f \t %.2f \t %.2f \n",
empno, name, bpay, allow, ded, bpay + allow - ded) ;
}
fclose(fptr) ;
getch() ;
}


RUN 1 :
~~~~~~~
Enter the number of employees : 2
Enter the employee number : 101
Enter the name : Udaya
Enter the basic pay, allowances & deductions : 6000 1000 500
Enter the employee number : 102
Enter the name : Priya
Enter the basic pay, allowances & deductions : 5000 1000 450

Emp. No. Name   Bpay      Allow     Ded     Npay

101          Udaya  6000.00 1000.00 500.00 6500.00
102          Priya    5000.00 1000.00 450.00 5550.00

No comments:

Post a Comment