Hi
I am trying to input data from a txt file
Sort it using structs
and out put it to another file
i am getting a lot of errors that i cant seem to figure out.
Note this is s section of a larger body of work
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#defineSIZE300
#defineemp_File_len211 // length of an employee file line
int exit_to_main; // allows to return to main menu
/*
********************************************************************************************
* Function Prototypes
*
********************************************************************************************
*/
void calSub(); //calc function called by load data
int load_data(); // function 1
void display_menu(); // Function 2 returns the user input
void add_new_employee();
void delete_employee();
//void editemployeeNum(); // used to modify files by Employee number
void view_all_employees();
void view_qualifying_employees();
void find_disqualified_employees();
void sort_employees_by_name();
void sort_employees_by_subsidy_amount();
void view_total_subsidy_amount();
void save_data(int, int); // function 11
/*
********************************************************************************************
* Structs
*
********************************************************************************************
*/
typedef struct {
int employee_code;
const char *employee_surName[20];
const char *employee_name[20];
int insurable_weeks;
float gross_pay;
float subsidy;
const char *qualify[3];
const char *reason[50];
} Employee;
Employee tempEmployee; // a global struct of type employee.data is read into an array of structs
Employee compare; // a global struct used to compare structs and elements of structs
Employee add;
Employee del;
Employee tempsort[500]; // used for sorting
Employee EMP[500]; // used for sorting
//** Array of structs******
Employee dynEMP[250]; // an array of structs for the employee
/*
********************************************************************************************
* Main Block
*
********************************************************************************************
*/
int main(void)
{
sort_employees_by_name();
return 0;
}
void sort_employees_by_name()
{
int i = 0, size, j;
FILE *cfPtr = fopen("subsidy.txt", "r");
char ch;
if (cfPtr == NULL) {
}
printf("\n Cannot Open File");
exit(0);
while (ch != EOF) {
(fscanf
(cfPtr, "%d%s%s%d%f%f%s%s", &(EMP.employee_code), EMP.employee_surName,
EMP.employee_name, &(EMP.insurable_weeks), &(EMP.gross_pay),
&(EMP.subsidy), &(EMP.qualify), &(EMP.reason)));
ch = fgetc(cfPtr);
i++;
}
size = i - 1;
for (i = 1; i < size; ++i)
for (j = 0; j < size - i; j++)
if (EMP[j + 1].surName < EMP[j].surName) {
tempsort = EMP[j];
EMP[j] = EMP[j + 1];
EMP[j + 1] = tempsort;
}
FILE *cfPtr2 = fopen("Name_Sort.txt", "w");
for (i = 0; i < size; i++)
fprintf(cfPtr2, "%15d%15s%15s%15d%15f%15f%15s%15s", EMP.employee_code,
EMP.employee_surName, EMP.employee_name, EMP.insurable_weeks,
EMP.gross_pay, EMP.subsidy, EMP.qualify, EMP.reason);
printf("\n The file is sorted successfully and saved as Name_Sort.txt \n\n");
}