Hello everyone, here is the program for sorting tha contents of a binary file containing records of various employees in ascending order according to name.
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
typedef struct emp
{
char name[20];
int age;
int salary;
}emp;
const char *file="employee.dat";
void main()
{
int i,j,n;
long int size;
emp e1,e2;
FILE *fp,*ft,*fs;
clrscr();
printf("Enter the number of employees: ");
scanf("%d",&n);
printf("Enter the details of employees:\n");
fp=fopen(file,"wb");
size=sizeof(e1);
for(i=1;i<=n;i++)
{
printf("Enter the name of %d employee: ",i);
scanf("%s",e1.name);
printf("Enter the age of %d employee: ",i);
scanf("%d",&e1.age);
printf("Enter the salary of %d employee: ",i);
scanf("%d",&e1.salary);
fwrite(&e1,sizeof(e1),1,fp);
fflush(stdin);
}
fclose(fp);
fp=fopen(file,"rb+");
printf("The contents of the file are:\n");
for(i=1;i<=n;i++)
{
fread(&e1,size,1,fp);
printf("%s\t%d\t%d\n",e1.name,e1.age,e1.salary);
}
fclose(fp);
fs=fopen("temps.dat","wb");
i=1;
while(i<=n)
{
fp=fopen(file,"rb");
fread(&e1,sizeof(e1),1,fp);
rewind(fp);
while(fread(&e2,sizeof(e2),1,fp))
{
if(strcmp(e1.name,e2.name)>0)
{
strcpy(e1.name,e2.name);
e1.age=e2.age;
e1.salary=e2.salary;
}
}
fwrite(&e1,sizeof(e1),1,fs);
i++;
ft=fopen("tempo.dat","wb");
rewind(fp);
while(fread(&e2,sizeof(e2),1,fp))
{
if(strcpy(e1.name,e2.name)!=0)
{
fwrite(&e2,sizeof(e2),1,ft);
}
}
fclose(fp);
fclose(ft);
remove(file);
rename("tempo.dat",file);
}
fclose(fp);
fclose(fs);
remove(file);
rename("temps.dat",file);
fp=fopen(file,"rb");
printf("\nThe contents of file after sorting:\n");
for(i=1;i<=n;i++)
while(fread(&e1,size,1,fp))
printf("%s\t%d\t%d\n",e1.name,e1.age,e1.salary);
}
fclose(fp);
getch();
}
#include<string.h>
#include<conio.h>
#include<stdlib.h>
typedef struct emp
{
char name[20];
int age;
int salary;
}emp;
const char *file="employee.dat";
void main()
{
int i,j,n;
long int size;
emp e1,e2;
FILE *fp,*ft,*fs;
clrscr();
printf("Enter the number of employees: ");
scanf("%d",&n);
printf("Enter the details of employees:\n");
fp=fopen(file,"wb");
size=sizeof(e1);
for(i=1;i<=n;i++)
{
printf("Enter the name of %d employee: ",i);
scanf("%s",e1.name);
printf("Enter the age of %d employee: ",i);
scanf("%d",&e1.age);
printf("Enter the salary of %d employee: ",i);
scanf("%d",&e1.salary);
fwrite(&e1,sizeof(e1),1,fp);
fflush(stdin);
}
fclose(fp);
fp=fopen(file,"rb+");
printf("The contents of the file are:\n");
for(i=1;i<=n;i++)
{
fread(&e1,size,1,fp);
printf("%s\t%d\t%d\n",e1.name,e1.age,e1.salary);
}
fclose(fp);
fs=fopen("temps.dat","wb");
i=1;
while(i<=n)
{
fp=fopen(file,"rb");
fread(&e1,sizeof(e1),1,fp);
rewind(fp);
while(fread(&e2,sizeof(e2),1,fp))
{
if(strcmp(e1.name,e2.name)>0)
{
strcpy(e1.name,e2.name);
e1.age=e2.age;
e1.salary=e2.salary;
}
}
fwrite(&e1,sizeof(e1),1,fs);
i++;
ft=fopen("tempo.dat","wb");
rewind(fp);
while(fread(&e2,sizeof(e2),1,fp))
{
if(strcpy(e1.name,e2.name)!=0)
{
fwrite(&e2,sizeof(e2),1,ft);
}
}
fclose(fp);
fclose(ft);
remove(file);
rename("tempo.dat",file);
}
fclose(fp);
fclose(fs);
remove(file);
rename("temps.dat",file);
fp=fopen(file,"rb");
printf("\nThe contents of file after sorting:\n");
for(i=1;i<=n;i++)
while(fread(&e1,size,1,fp))
printf("%s\t%d\t%d\n",e1.name,e1.age,e1.salary);
}
fclose(fp);
getch();
}
No comments:
Post a Comment