Write a C program that prompts the user to enter the name of a data file that co
ID: 3566288 • 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:
?hUse strcat to merge the last name with the first name with a comma between
?hProgram without sort options is a 10 point deduction.
?hProgram with no output to report5.txt is a 10 point deduction
?hWithout current date and time stamp is a 2 point deduction
?hNo data validation is required (assume it was validated before writing to the file)
?hYou must open the file in rb mode: in_file_ptr = fopen(file_name, "rb")
?hI 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#define EMPLOYEES 100
#define BUFFER 256
struct emp
{
int id_num;
float salary;
char first_name[20];
char last_name[30];
};
void print_employee_report(struct emp *emp_data, int num, FILE* out_file);
void swap(void *first, void *second, size_t n);
int main(void)
{
struct emp emp_data[EMPLOYEES];
char file_name[BUFFER];
FILE *in_file_ptr, *out_file_ptr;
char choice;
printf("Welcome to Employee Center ");
printf("Enter File Name: ");
fgets(file_name,BUFFER, stdin);
file_name[strlen(file_name) - 1] = '';
in_file_ptr = fopen(file_name, "rb");
if (in_file_ptr == NULL)
{
printf("Error opening %s. ", file_name);
return EXIT_FAILURE;
}
int num = 0;
while(!feof(in_file_ptr) && num < EMPLOYEES)
{
fread(&emp_data[num++], sizeof(emp_data), num, in_file_ptr);
num++;
}
fclose(in_file_ptr);
do
{
int c;
printf("Sort by: Employee Number(N), Name(A), or Balance(B): ");
choice = getchar();
while ((c = fgetc(stdin)) != ' ')
choice = toupper(choice);
if(choice != 'N' && choice != 'A' && choice != 'B')
printf("Invalid choice! Enter only N/A/B: ");
}while(choice != 'N' && choice != 'A' && choice != 'B');
for(int i = 0; i < num; ++i)
{
int insert = i;
while(insert > 0)
{
int stop = 0;
switch (choice)
{
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(emp_data));
--insert;
}
}
print_employee_report(emp_data, num, stdout);
out_file_ptr = fopen("report5.txt", "w");
if(out_file_ptr == NULL)
{
printf("(Error creating out_file file report5.txt) ");
return EXIT_FAILURE;
}
print_employee_report(emp_data, num, out_file_ptr);
printf("(Report has been sent to report5.txt) ");
printf("Thank you for using the program. ");
fclose(out_file_ptr);
return EXIT_SUCCESS;
}
void swap(void *first, void *second, size_t n)
{
void *temp = malloc(n);
if (temp == NULL)
exit(1);
memcpy(temp, first, n);
memcpy(first, second, n);
memcpy(second, temp, n);
free(temp);
}
void print_employee_report(struct emp *emp_data, int num, FILE *out_file)
{
char cname[50];
time_t current_time = time(NULL);
fprintf(out_file, "Employee Salary Report %s", ctime(¤t_time));
fprintf(out_file, "Employee No. Name Salary ");
for(int i = 0; i < num; ++i)
{
sprintf(cname, "%s, %s", emp_data[i].last_name, emp_data[i].first_name);
fprintf(out_file, "%12d %-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.