C: Sorting and searching data from separated files

Click For Summary

Discussion Overview

The discussion revolves around a programming homework assignment that involves reading employee data from a binary file, sorting it using an insertion sort algorithm, and searching for specific employee information using a linear search algorithm. Participants are analyzing the provided code for syntax errors and logical issues.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant points out that there are syntax errors in the code.
  • Another participant asks for clarification on the nature of the problems, including whether the code compiles, executes, or produces error messages.
  • Specific segmentation faults and error messages are listed, including issues with incompatible types and invalid operations.
  • A participant highlights that the assignment of an entire array of employees to a single employee variable is incorrect.
  • Concerns are raised about the logic of using 'i != EOF' in the loop condition, suggesting it may not be appropriate.

Areas of Agreement / Disagreement

Participants generally agree that there are multiple syntax and logical errors in the code, but there is no consensus on the exact nature of all issues or how to resolve them.

Contextual Notes

Limitations include potential misunderstandings of C syntax, the handling of structure assignments, and the use of EOF in loop conditions. Specific assumptions about the data structure and file contents are not fully explored.

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   Reactions: gruba

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K