C: Sorting and searching data from separated files

AI Thread Summary
The discussion focuses on a programming assignment that involves reading employee data from a binary file, sorting it by ID using an insertion sort algorithm, and searching for an employee by ID using a linear search. The provided code has several syntax errors and logical issues, including incorrect assignments between arrays and single structures, and improper loop conditions. Specifically, the insertion sort function attempts to assign an entire array to a single structure, leading to type incompatibility errors. Additionally, the condition for the loop in the insertion sort is incorrectly set to check against EOF, which is not appropriate for array indexing. The discussion emphasizes the need for careful handling of structure assignments and proper loop conditions to resolve these issues.
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:
Physics news 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 gruba

Similar threads

Replies
2
Views
2K
Replies
3
Views
1K
Replies
3
Views
2K
Replies
7
Views
3K
Replies
1
Views
3K
Replies
1
Views
6K
Replies
4
Views
2K
Replies
7
Views
2K
Back
Top