1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C: Sorting and searching data from separated files

  1. Dec 20, 2015 #1
    1. The problem statement, all variables and given/known data
    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.
    2. Relevant equations
    3. The attempt at a solution

    I need a clarification on what is wrong with functions for sorting and searching in the following code:
    Code (Text):

    #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: Dec 20, 2015
  2. jcsd
  3. Dec 20, 2015 #2
    It seems to me there are some syntax errors in the code.
     
  4. Dec 20, 2015 #3

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    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?
     
  5. Dec 20, 2015 #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'|
     
  6. Dec 20, 2015 #5

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    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 ].
    Code (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: Dec 20, 2015
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C: Sorting and searching data from separated files
Loading...