Store 10 employees record in a data file and display who gets lowest salary - C

Q. 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] 
#AssignmentNo1
==> Code goes like this:

#include<stdio.h>
#define SIZE 3
int main()
{
    FILE *fptr;
    fptr=fopen("D:\\employeee.dat","w+");
    if(fptr==NULL)
    {
        printf("File can't be created");
        exit(1);
    }
    int i,j;
    float tempforsalary;
    struct employee
    {
        int id;
        char name[20];
        float salary;
    };
    struct employee emp[SIZE],temp;
   
    for ( i = 0; i < SIZE; i++)
    {
        printf("Employee Id: ");
        scanf("%d",&emp[i].id);
        printf("Employee Name: ");
        scanf("%s",emp[i].name);
        printf("Employee Salary: ");
        scanf("%f",&tempforsalary);
        emp[i].salary=tempforsalary;
        fflush(stdin);
        fprintf(fptr,"Emp Id:\t Emp Name:\t Emp Salary:\n");
        fprintf(fptr,"%d\t %s\t %.2f",emp[i].id,emp[i].name,emp[i].salary);
        printf("\n");
    }
    fclose(fptr);

    for ( i = 0; i < SIZE-1; i++)
    {
        for ( j = i+1; j < SIZE; j++)
        {
            if (emp[i].salary>emp[j].salary)
            {
                temp=emp[i];
                emp[i]=emp[j];
                emp[j]=temp;
            }
        }
    }
    printf("Employee having lowest salary: \n");
    printf("Id: %d\t Name:%s\t Salary:%.2f\n",emp[0].id,emp[0].name,emp[0].salary);
    return 0;
}

Output:














If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
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