Store records of 10 employees in employee.dat file and disply the record having lowest salary- C

Q. 1) WAP to input 10 employee records(Emp_id,Emp_name,Emp_salary) and store them in a
datafile named Employee.dat. Display the employee who gets lowest salary. (12)

==> Code goes like this:

#include <stdio.h>
#define SIZE 10
int main() {
    FILE *fptr;
    fptr=fopen("D:\\employee.dat","w");
    struct employee
    {
        char empName[30];
        int empId;
        float empSalary;
    };
    struct employee emp[SIZE],temp;
    int i,j;
    float sal;
    for ( i = 0; i <SIZE ; ++i) {
        printf("\n Enter Information of Employee No %d\n",i+1);
        printf("\n Name:\t");
        gets(emp[i].empName);
        printf("\n Emp id:\t");
        scanf("%d",&emp[i].empId);
        printf("\n Salary:\t");
        scanf("%f",&sal);
        emp[i].empSalary=sal;
        fflush(stdin);
        //writing information to file
        fprintf(fptr,"\nName=%s\tId=%d\tSalary=%.2f\n",emp[i].empName,emp[i].empId,emp[i].empSalary);
    }
    for ( i = 0; i < SIZE-1; i++) {
        for(j=i+1;j<SIZE;j++)
        {
            if(emp[i].empSalary>emp[j].empSalary)
            {
                temp=emp[i];
                emp[i]=emp[j];
                emp[j]=temp;
            }
        }
    }
    printf("\n Employee having the lowest salary:\n");
    printf("\n Employee Name:\tEmployee id:\tEmployee Salary:\n");
    printf("\n------------------------------------------------------------\n");
    printf("\n %s\t%d\t%.2f",emp[0].empName,emp[0].empId,emp[0].empSalary);
    fclose(fptr);
    return 0;
}

Output







































































Content of D:\employee.dat



















If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment