C: Sorting and searching data from separated files

In summary: I think you are trying to say that you want to read until the end of the file and so you should test the return value of the read function.Still another problem is that you are using a variable, x, that you have not declared. You must declare all variables before you use them.
  • #1
gruba
206
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
  • #2
It seems to me there are some syntax errors in the code.
 
  • #3
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?
 
  • #4
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'|
 
  • #5
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

Related to C: Sorting and searching data from separated files

What is sorting and searching data from separated files?

Sorting and searching data from separated files is the process of organizing data stored in different files based on specific criteria, such as alphabetical order or numerical value, and then searching for specific data within those files.

Why is sorting and searching data from separated files important?

Sorting and searching data from separated files allows for efficient and quick access to specific data, making it easier to analyze and draw conclusions from large amounts of information.

What are some common sorting algorithms used for organizing data from separated files?

Some common sorting algorithms include bubble sort, selection sort, insertion sort, merge sort, quicksort, and heapsort. Each algorithm has its own advantages and disadvantages, depending on the type and size of data being sorted.

What is the difference between sorting and searching data from separated files?

Sorting data from separated files involves organizing the data in a specific order, while searching data involves looking for specific data within the organized files. In other words, sorting is the process of arranging data, while searching is the process of finding data.

How can I ensure that my sorted and searched data is accurate?

To ensure accuracy, it is important to carefully select the sorting and searching algorithms that best fit your data and to regularly test and validate the results. It is also helpful to have a good understanding of your data and any potential errors or inconsistencies that may affect the accuracy of the results.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top