Write a C program that prompts the user to enter the name of a data file that co
ID: 3565915 • Letter: W
Question
Write a C program that prompts the user to enter the name of a data file that contains
some data regarding some employees. The file contains data that you will store in a struct
format defined by:
struct emp
{
int id_num; /* employee number */
float salary; /* employee salary */
char first_name[20]; /* employee first name */
char last_name[30]; /* employee last name */
}
I called mine: struct emp jim[100]; (you may use any name you wish)
I will let you decide the appropriate prompts and edit messages.
You will read the data from the data file and store the info in an array.
Assume no more than 100 records will be in the data file.
You may use the data below for testing purposes with the attached file.
Once the program opens the file, and reads the data into the array, the program
should then SORT THE DATA IN ASCENDING ORDER BY ID NUMBER, OR NAME OR
BALANCE (by user choice), and output the Employee data in report form of your design
with alignment, or exactly as follows BELOW.
The report will also be directed to an output text file (named report5.txt) with the same
output as printed to the screen.
Hints/requirements:
? Use strcat to merge the last name with the first name with a comma between
? Program without sort options is a 10 point deduction.
? Program with no output to report5.txt is a 10 point deduction
? Without current date and time stamp is a 2 point deduction
? No data validation is required (assume it was validated before writing to the file)
? You must open the file in rb mode: in_file_ptr = fopen(file_name, "rb")
? I read the file: fread (&jim[x] , sizeof(jim[x]),1 ,in_file_ptr);
The sample data file contains the following data (in a binary format)
121 512.00 Jon Smith
123 1022.00 John Doe
163 2023.00 Jim Beam
183 3932.00 Jack Daniels
323 922.00 Bill Bailey
Sample I-O below - user responses are in coral:
Welcome to Employee Center
Enter File Name: newdata.dat
5 employees
Sort by: Employee Number(N), Name(A), or Balance(B): a
Employee Salary Report Wed Aug 13 10:42:26 2014
Employee
No. Name Salary
323 Bailey, Bill 922.00
163 Beam, Jim 2023.00
183 Daniels, Jack 3932.00
123 Doe, John 1022.00
121 Smith, Jon 512.00
(Report has been sent to report5.txt)
Thank you for using the program.
Explanation / Answer
Let me know if you don't understand any part of the code and I can explain further! I wasn't sure if using strcat was a requirement, but if it is just replace sprintf in print_employee_report with a few strcat calls. Since Chegg doesn't format code very well, I've also put it here: http://pastebin.com/SQywTzJX. I also made the sample data into a binary data file, which you can use to test the program: https://drive.google.com/file/d/0B9T5JWcch2GfN0JYNGptejU5WE0/view?usp=sharing.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#define MAX_EMPLOYEES 100
#define INPUT_BUFFER_SIZE 256
typedef struct {
int id_num; /* Employee number */
float salary; /* Employee salary */
char first_name[20]; /* Employee first name */
char last_name[30]; /* Employee last name */
} Employee;
void swap(void *o1, void *o2, size_t s);
void print_employee_report(Employee *emp_data, int nemp, FILE* output);
int main(void) {
Employee emp_data[MAX_EMPLOYEES]; /* Array of employee structs to store data */
char filename[INPUT_BUFFER_SIZE]; /* Employee data filename string */
FILE *efp, *ofp; /* Employee input and output file pointers */
int nemp = 0; /* Total number of employees */
char sort_letter; /* Character to determine how to sort output */
int i;
/* Get the filename */
printf("Welcome to Employee Data Center ");
printf("Enter file name: ");
fgets(filename, INPUT_BUFFER_SIZE, stdin);
filename[strlen(filename) - 1] = '';
/* Read the file */
efp = fopen(filename, "rb");
if (efp == NULL) {
printf("Error opening %s. ", filename);
return EXIT_FAILURE;
}
while (!feof(efp) && nemp <= MAX_EMPLOYEES) {
fread(&emp_data[nemp++], sizeof(Employee), 1, efp);
}
--nemp;
fclose(efp);
/* Get desired sorting method from user */
do {
int c;
printf("Sort by: Employee Number(N), Name(A), or Balance(B): ");
sort_letter = getchar();
while ((c = fgetc(stdin)) != ' ')
;
sort_letter = toupper(sort_letter);
if (sort_letter != 'N' && sort_letter != 'A' && sort_letter != 'B')
printf("Error: invalid menu choice. Please enter N, A, or B ");
} while (sort_letter != 'N' && sort_letter != 'A' && sort_letter != 'B');
/* Sort the data ascendingly according to the menu choice (insertion sort) */
for (i = 1; i < nemp; ++i) {
int insert = i;
while (insert > 0) {
int stop = 0;
switch (sort_letter) {
case 'N':
if (emp_data[insert].id_num > emp_data[insert - 1].id_num)
stop = 1;
break;
case 'A':
if (strcmp(emp_data[insert].last_name, emp_data[insert - 1].last_name) > 0)
stop = 1;
break;
case 'B':
if (emp_data[insert].salary > emp_data[insert - 1].salary)
stop = 1;
break;
}
if (stop)
break;
swap(&emp_data[insert], &emp_data[insert - 1], sizeof(Employee));
--insert;
}
}
/* Display the sorted employee report */
print_employee_report(emp_data, nemp, stdout);
/* Write the sorted employee report to report5.txt */
ofp = fopen("report5.txt", "w");
if (ofp == NULL) {
printf("(Error creating output file report5.txt) ");
return EXIT_FAILURE;
}
print_employee_report(emp_data, nemp, ofp);
fclose(ofp);
printf("(Report has been sent to report5.txt) ");
printf("Thank you for using this program. ");
return EXIT_SUCCESS;
}
/*
* swap - Swaps memory locations 'o1' and 'o2' over size 's'
*/
void swap(void *o1, void *o2, size_t s) {
void *temp = malloc(s);
if (temp == NULL)
exit(1);
memcpy(temp, o1, s);
memcpy(o1, o2, s);
memcpy(o2, temp, s);
free(temp);
}
/*
* print_employees - Print an employee report from an array of Employee structs
*/
void print_employee_report(Employee *emp_data, int nemp, FILE *output) {
int i;
char cname[50];
time_t current_time = time(NULL);
fprintf(output, "Employee Salary Report %s", ctime(¤t_time));
fprintf(output, "Employee No. Name Salary ");
for (i = 0; i < nemp; ++i) {
sprintf(cname, "%s, %s", emp_data[i].last_name, emp_data[i].first_name);
fprintf(output, "%12i %-24s %.2f ", emp_data[i].id_num, cname, emp_data[i].salary);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.