Write a program that reads data from binary file EMPLOYEE.DAT (employees are proffesors and teaching assistants, assume that the file exists). Split data about proffesors and teaching assistants in two separate text files and sort data by ID using insertion sort algorithm. Search and print data about employee which ID is entered using linear search algorithm. Employee data: ID,NAME,SURNAME,TITLE,SALARY.
I need a clarification on what is wrong with functions for sorting and searching in the following code:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef struct
{
char ID[20];
char surname[20],name[20];
char title[20];
double salary;
}EMPLOYEE;
void printEmployeeToFile(EMPLOYEE empl,FILE *ptr_f,int ord_num)
{
fprintf(ptr_f,"%2d. %-19s %-19s %-19s %-19s %6.2lf",
ord_num,empl.ID,empl.name,empl.surname,empl.title,empl.salary);
}
void insertionSort(EMPLOYEE *arr)
{
int i,j;
EMPLOYEE x[1000];
for(i=0; i != EOF; i++)
{
x=arr[i];
for(j=i; j > 0; && x < arr[j-1]; j--)
arr[j].ID = arr[j-1].ID;
arr[j]=x;
}
}
void split(FILE *empl_ptr,FILE *proffesors,FILE *teaching_assistants)
{
char temp[100];
int prof_ord_num=0,ta_ord_num=0;
EMPLOYEE empl;
while(fread(&empl,sizeof(EMPLOYEE),1,empl_ptr))
{
if(!strcmp(empl.title,"proffesor"))
printEmployeeToFile(empl,proffesors,++prof_ord_num);
else if(!strcmp(empl.title,"teaching assistant"))
printEmployeeToFile(empl,teaching_assistants,++ta_ord_num);
}
}
int linearSearch(EMPLOYEE *arr,char *key)
{
int i=0;
while(strcmp(arr[i].ID,key))
i++;
return i;
}
int main()
{
FILE *empl_ptr,*proffesors,*teaching_assistants;
EMPLOYEE *arr;
EMPLOYEE empl;
arr=(EMPLOYEE*)malloc(1000 * sizeof(EMPLOYEE));
int i
if((empl_ptr = fopen("EMPLOEE.DAT","rb")) && (proffesors = fopen("PROFFESORS.TXT","w")) &&
(teaching_assistants = fopen("TEACHING_ASSISTANTS.TXT","w")))
{
insertionSort(arr);
split(empl_ptr,proffesors,teaching_assistants);
fclose(empl_ptr);
fclose(proffesors);
fclose(teaching_assistants);
}
printf("Enter ID for searching");
scanf("%s",empl.ID);
printf("Searching result:");
i=linearSearch(arr,empl.ID);
free(arr);
return 0;
}