C program to display employee details in the order of salary from file employee.txt which store employee name, id and salary
C program to display employee details in the order of salary from file employee.txt which store employee name, id and salary
C Source Code: Sorting Employee Records On The Basis of Salary
#include<stdio.h>
typedef struct
{
char name[30];
int id;
int salary;
}employee;
int main()
{
int n,i,j;
FILE *fptr;
employee e[10], temp;
/* Reading file in binary read mode */
fptr = fopen("employee.txt","rb");
if(fptr == NULL)
{
printf("File error!");
exit(1);
}
printf("Enter how many records:\n");
scanf("%d",&n);
/* Reading from file and storing them in structure array */
for(i=0;i < n;i++)
{
fread(&e[i],sizeof(e[i]),1, fptr);
}
/* Sorting */
for(i=0;i< n-1;i++)
{
for(j=i+1;j< n;j++)
{
if(e[i].salary>e[j].salary)
{
temp = e[i];
e[i] = e[j];
e[j] = temp;
}
}
}
/* Displaying sorted list */
for(i=0;i< n;i++)
{
printf("Name = %s\tId = %d\tSalary = %d\n",e[i].name, e[i].id, e[i].salary);
}
fclose(fptr);
return 0;
}