C: Sorting and searching data from separated files

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 2K views
gruba
Messages
203
Reaction score
1

Homework Statement


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.

Homework Equations


3. The Attempt at a Solution [/B]
I need a clarification on what is wrong with functions for sorting and searching in the following code:
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 printHeaderToFile(FILE *ptr_f)
{
  fprintf(ptr_f,"============== =================== =================== =================== =================== ======\n");
  fprintf(ptr_f,"ORDINAL NUMBER ID  NAME  SURNAME  TITLE  SALARY\n");
  fprintf(ptr_f,"============== =================== =================== =================== =================== ======\n");
}

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 printFooterToFile(FILE *ptr_f)
{
  fprintf(ptr_f,"============== =================== =================== =================== =================== ======");
}

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;
  fgets(temp,100,empl_ptr);
  fgets(temp,100,empl_ptr);
  fgets(temp,100,empl_ptr);
  printHeaderToFile(proffesors);
  printHeaderToFile(teaching_assistants);
  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);
  }
  printFooterToFile(proffesors);
  printFooterToFile(teaching_assistants);
}

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;
}
 
Last edited:
on Phys.org
It seems to me there are some syntax errors in the code.
 
For a start, why don't you tell us what is wrong. Does it compile? link? execute? give a wrong answer? abort with an error message? What is the nature of the problem and what are the symptoms / error messages?
 
Segmentation faults:

|37|error: incompatible types when assigning to type 'struct EMPLOYEE[1000]' from type 'EMPLOYEE'|
|38|error: invalid operands to binary < (have 'void *' and 'EMPLOYEE')|
|38|error: expected ')' before ';' token|
|39|error: incompatible types when assigning to type 'char[20]' from type 'char *'|
|40|error: incompatible types when assigning to type 'EMPLOYEE' from type 'struct EMPLOYEE *'|
|38|error: label 'x' used but not defined|
|80|error: expected '=', ',', ';', 'asm' or '__attribute__' before 'if'|
 
Mod note: Added code tags and fixed unwanted italics problem.
The first error message says that there is an error on line 37 where you are trying to set the values of an entire array of employees to the values of a single employee.
|37|error: incompatible types when assigning to type 'struct EMPLOYEE[1000]' from type 'EMPLOYEE'|

Here is your code. x is an array of 1000 employees and you are trying to set it equal to a single employee, arr[ i ].
C:
 EMPLOYEE x[1000];
  for(i=0; i != EOF; i++)
  {
  x=arr[ i ] ;
Another possible problem is that assignments of structures like EMPLOYEE can not copy values. You must copy the information part-by-part or use a pointer.
Another problem is the test if i != EOF. I don't think that makes sense.
 
Last edited by a moderator:
  • Like
Likes   Reactions: gruba