Question: Modify the C program is split into two source files and one header file. In this project, you are allowed to modify all parts of

Modify the C program is split into two source files and one header file. In this project, you are allowed to modify all parts of the program.
Put all functions related to operations on the list of guests into volunteer.c
Create a header file named volunteer.h that contains struct volunteer declaration and prototypes for the functions in volunteer.c. The header file should enclose the contents of the header file in an #ifndef-#endif pair to protect the file.
Put read_line function in read_line.c
Create a header file named read_line.h that contains function prototype for read_line function. The header file should enclose the contents of the header file in an #ifndef-#endif pair to protect the file.
#include
#include
#include
#define MaxClrLength 100
#define InputFileName "supply.csv"
#define OutputFileName "result.csv"
#define MaxSupplyItems 200
#define MaxNameLength 100
struct supply {// struct to hold information
char name[MaxNameLength];
char color[MaxClrLength];
int quantity;
};
int compare(const void *a, const void *b){
const struct supply *supply_a =(const struct supply *)a;
const struct supply *supply_b =(const struct supply *)b;
int name_cmp = strcmp(supply_a->name, supply_b->name); // compare name
if (name_cmp !=0){
return name_cmp;
}
return strcmp(supply_a->color, supply_b->color); // compare by color if names are the same
}
int read_line(FILE *file, struct supply supplies[]){// read line function
int count =0;
while (fscanf(file,"%[^,],%[^
],%d
", supplies[count].name, supplies[count].color, &supplies[count].quantity)==3){
count++; // reads line until eof and increment count
}
return count; // Return total # of supplies
}
void WriteResult(struct supply result[], int n){// Function to write search results to an output file
FILE *file = fopen(OutputFileName,"w"); // File in write mode
if (file == NULL){
printf("Error opening file.
"); // Error message if fails to open
return;
}
for (int i =0; i < n; i++){// Write supply info to output file
fprintf(file,"%s,%s,%d
", result[i].name, result[i].color, result[i].quantity);
}
fclose(file); // Close the output file
}
int main(){
struct supply supplies[MaxSupplyItems]; // Array to store supplies from the input file
FILE *file = fopen(InputFileName,"r"); // Open input file in read mode
if (file == NULL){
printf("Error opening file.
"); // Error message if file opening fails
return 1;
}
int NumSupplies = read_line(file, supplies); // Looks up supply information from the input file
fclose(file);
qsort(supplies, NumSupplies, sizeof(struct supply), compare); // Sort supplies using quicksort
WriteResult(supplies, NumSupplies); // Write sorted supplies to the output file
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!