Any help and or assistance would be appreciated I've already tried a bunch of stuff but I am very confused I have been working on this for at least 10 hours over the last two days
what I need to add
Add file i/o to your program
Implement a function writeEmpToFile that takes two arguments: a struct Employee pointer and a FILE *.
It should write each field in order as an appropriate type. Note that you will probably want to write a length of the name before you write the characters of the name. By doing this, when you write the load function below, you can read the length of the string and use it to malloc a buffer of the proper size to hold the name.
Because of the embedded name pointer, you CAN NOT write the Employee struct as a single struct. You will need to write it out field by field. You will want to write this as a binary file, not as a text file.
Implement a SAVE command in your main loop that will save all the employees out to a file. The SAVE command should ask for a file name, similar to the way your FIND command asked for a name.
Implement a function readEmpFromFile that takes a FILE * as the only argument and returns a pointer to a struct employee. This function should read the information from the file (reversing what writeEmpToFile does), create a new employee and fill in the data. If the FILE has no more info (is at end of file), this function should return NULL. This function must also ensure any employee actually created (not the NULL) is added to the array (as is done in createEmployee).
Change main to load employees from a file if a command line argument is given to the program (using your readEmpFromFile function you just wrote).
my code that I currently have you can use it as a foundation
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define EMPS_SIZE 20
#define SSN_SIZE 9
#define MAX_EMPS 19
typedef struct {
int salary;
int yearBorn;
char ssn[SSN_SIZE];
char * name;
}
Employee;
Employee * emps[EMPS_SIZE];
int totalEmps = 0;
void displayEmployee(Employee * person) {
printf("\nYou entered the name: %s\nYou entered the year: %d\nYou entered the social security number: ", person -> name, person -> yearBorn);
for (int i = 0; i < SSN_SIZE; i++) {
if (i == 2) {
printf("%c-", person -> ssn[i]);
} else if (i == 4) {
printf("%c-", person -> ssn[i]);
} else if (i == 9) {
printf("%c ", person -> ssn[i]);
} else {
printf("%c", person -> ssn[i]);
}
}
int num = person -> salary / 1000;
int num2 = person -> salary % 1000;
printf("\nYou entered the salary: $%d,%03d\n\n\n", num, num2);
}
void readEmployee(Employee * person) {
int * inputYearBorn = & person -> yearBorn;
int * inputSalary = & person -> salary;
char buff[256];
printf("Welcome to the add employee manager!\n\n");
printf("Enter the Employees name: ");
scanf("%s", buff);
person -> name = malloc(strlen(buff + 1));
strcpy(person -> name, buff);
printf("Enter the year the Employee was born: ");
scanf("%d", inputYearBorn);
printf("Enter the salary the Employee makes: ");
scanf("%d", inputSalary);
printf("Enter the ssn of the Employee: ");
scanf("%s", buff);
* person -> ssn = '\0';
strncat(person -> ssn, buff, 9);
}
void createEmployee(void) {
if (totalEmps <= MAX_EMPS) {
for (int i = 0; i <= MAX_EMPS; ++i) {
if (emps[i] == NULL) {
Employee * newEmployee = (Employee * ) malloc(sizeof(Employee));
emps[i] = newEmployee;
totalEmps++;
readEmployee(newEmployee);
break;
}
}
} else {
printf("You cannot hire anymore employees\n\n");
}
}
void releaseEmployee(Employee * person) {
free(person -> name);
free(person);
}
void listEmployees(void) {
for (int i = 0; i <= MAX_EMPS; i++) {
if (emps[i] == NULL) {
printf("No employee at : %d\n", i);
} else {
printf("Employee position in list : %d\n", i);
displayEmployee(emps[i]);
}
}
printf("Thats all the employees!\n\n");
}
Employee * findEmployee(char * name) {
printf("You entered the name: %s\n\n", name);
int counter = 0;
for (int i = 0; i < totalEmps; i++) {
if (!strcmp(emps[i] -> name, name)) {
printf("%s is an employee! They are at: %d\n", name, i);
return emps[i];
} else {
counter++;
}
}
printf("This employee doesn't exist\n");
return NULL;
}
void fireEmployee(char * name) {
printf("You entered the name: %s\n\n", name);
int counter = 0;
for (int i = 0; i < totalEmps; i++) {
if (!strcmp(emps[i] -> name, name)) {
printf("%s is now fired! They were at: %d\n", name, i);
free(emps[i]);
emps[0] = NULL;
return;
} else {
counter++;
}
}
printf("This employee doesn't exist\n");
return;
}
int salaryComp(Employee ** a, Employee ** b) {
Employee * empA = * a;
Employee * empB = * b;
if ( & empA -> salary == NULL && & empB -> salary != NULL) {
printf("I got here 2\n\n");
return 1;
} else if ( & empB -> salary == NULL && & empA -> salary != NULL) {
printf("I got here 3\n\n");
return -1;
} else if ( & empA -> salary == NULL && & empB -> salary == NULL) {
printf("I got here 4\n\n");
return -1;
} else {
printf("empA salary == %d\n", empA -> salary);
printf("empB salary == %d\n", empB -> salary);
return (empB -> salary - empA -> salary);
}
}
int nameComp(Employee ** a, Employee ** b) {
Employee * empA = * a;
Employee * empB = * b;
if (empA == NULL && empB != NULL) {
return 1;
} else if (empB == NULL && empA != NULL) {
return -1;
} else if (empA == NULL && empB == NULL) {
return -1;
} else {
return 0;
}
}
void sort(Employee * base[], int n, int( * compareFunc)(Employee ** , Employee ** )) {
qsort((void ** ) base, n, sizeof(void * ), (int( * )(const void * ,
const void * )) compareFunc);
}
void main(void) {
int end = 1;
char * command;
char buff[256];
char * findEmp;
char findBuff[256];
char * sortType;
char sortBuff[256];
char hire[] = "HIRE";
char list[] = "LIST";
char quit[] = "QUIT";
char find[] = "FIND";
char fire[] = "FIRE";
char _sort[] = "SORT";
char salary[] = "SALARY";
char name[] = "NAME";
printf("Welcome to the Employee Manager dashboard!\n");
while (end) {
printf("Would you like to HIRE, LIST, FIND, FIRE, SORT, or QUIT?\n\n");
scanf("%s", buff);
command = malloc(strlen(buff + 1));
strcpy(command, buff);
printf("You entered the command: %s\n\n", command);
if (!strcmp(command, hire)) {
createEmployee();
} else if (!strcmp(command, list)) {
listEmployees();
} else if (!strcmp(command, find)) {
printf("Enter an employee name, to find if they are a current employee:\n\n");
scanf("%s", findBuff);
findEmp = malloc(strlen(findBuff + 1));
strcpy(findEmp, findBuff);
findEmployee(findEmp);
} else if (!strcmp(command, fire)) {
printf("Enter an employee name, to fire\n\n");
scanf("%s", findBuff);
findEmp = malloc(strlen(findBuff + 1));
strcpy(findEmp, findBuff);
fireEmployee(findEmp);
} else if (!strcmp(command, _sort)) {
printf("Would you like to sort by SALARY or NAME?\n\n");
scanf("%s", sortBuff);
sortType = malloc(strlen(sortBuff + 1));
strcpy(sortType, sortBuff);
if (!strcmp(sortType, name)) {
printf("sorting by name\n\n");
sort(emps, EMPS_SIZE, nameComp);
listEmployees();
} else if (!strcmp(sortType, salary)) {
printf("sorting by salary\n\n");
sort(emps, EMPS_SIZE, salaryComp);
listEmployees();
} else {
printf("Improper sort type entered, exiting to main menu\n\n");
}
} else if (!strcmp(command, quit)) {
printf("Quitting the managers dashboard!\n\n");
end = 0;
} else {
printf("Please enter a proper command\n\n");
}
}
}
What I have tried:
everything I could think of but nothing has worked I have been trying to do this for the past 2 days