My Question starts here In the code linked 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 Question starts here
In the code linked 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).
Question ends here
To make it easier so that you do not have to go through the entire code I'm going to put writeEmpToFile function right here after the question but if you still need to look at the full code to get an understanding of what you are working with its linked at the bottom
void writeEmpToFile(Employee *emp, FILE *f) {
if(f!=NULL){
for (int i = 0; i < MAX_EMPS; i++) {
if (emps[i] != NULL) {
fwrite(&emps[i]->salary, sizeof(emps[i]->salary), 1, f);
fwrite(&emps[i]->yearBorn, sizeof(emps[i]->yearBorn), 1, f);
fwrite(& emps[i]->ssn, sizeof(char), SSN_SIZE, f);
int len = strlen(emps[i]->name) + 1;
fwrite(&len, sizeof(int), 1, f);
fwrite(&emps[i]->name, sizeof(emps[i]->name), len, f);
}
}
printf("Data written succesfully\n");
fclose(f);
}
else{
printf("Error while opening file\n");
}
}
https://onlinegdb.com/jYuB3gpCx
Trending now
This is a popular solution!
Step by step
Solved in 2 steps