Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a void function that will merge the contents of two text files containing

ID: 3559467 • Letter: W

Question

write a void function that will merge the contents of two text files containing chemical elements sorted by atomic number and will produce a sorted file by atomic number and produces a sorted file of elements. The funcion's parameters are references to the two input streams and the ouput stream. Each text file line contain for example:

11     Sodium       Na 22.99

20 Calcium     Ca 40.08

The function can assume that one file does not have two copies of the same element and that the binary output files should have this same property. When one of the input files is exhausted, do not forget to copy the remaining elements of the other input file to the result file. Please comment for each line to be able to understand.

Explanation / Answer

Structure

typedef struct{
int atomic_number;
char name[MAX];
char symbol[MAX];
double weight;
}chemical_t;
-----------------------------------------------------------------------

void merge(FILE *element_1, FILE *element_2, FILE *element){

chemical_t chemical;
chemical_t chemical_1;
chemical_t chemical_2;

element_1 = fopen("text.txt", "r");
element_2 = fopen("text1.txt", "r");
element = fopen("text.bin", "wb");


fscanf(element_1,"%d%s%s%lf",&chemical_1.atomic_number,
chemical_1.name,
chemical_1.symbol,
&chemical_1.weight);
fscanf(element_2,"%d%s%s%lf",&chemical_2.atomic_number,,
chemical_2.name,
chemical_2.symbol,
&chemical_2.weight);

while(!feof(element_1)){
if(chemical_1.atomic_number < chemical_2.atomic_number){
fwrite(&chemical_1, sizeof(chimico_t), 1, element);
fscanf(element_1,"%d%s%s%lf",&chemical_1.atomic_number,
chemical_1.name,
chemical_1.symbol,
&chemical_1.weight);
}

else{
while(!feof(element_2)){
fwrite(&chemical_2, sizeof(chimico_t), 1, element);
fscanf(element_2,"%d%s%s%lf",&chemical_2.atomic_number,,
chemical_2.name,
chemical_2.symbol,
&chemical_2.weight);
}
}
}



fclose(element_1);
fclose(element_2);
fclose(element);


}