Hi, i merged two files into chem.out but i need to figure out how I would arrange them by atomic number and how i can eliminate the duplicates. Should I use structures?
here is the program i wrote so far,
#include<stdio.h>
#include<math.h>
#include<string.h>
void merge_chem(FILE * inp1, FILE* inp2, FILE* outp);
int main(void)
{
FILE *inp1, *inp2, *outp;
inp1 = fopen("chem1.txt", "r");
inp2 = fopen("chem2.txt", "r");
outp = fopen("chem.out", "w");
merge_chem(inp1,inp2,outp);
fclose(inp1);
fclose(inp2);
fclose(outp);
return 0;
}
void merge_chem(FILE * inp1, FILE* inp2, FILE* outp)
{
int atomic_num[40], atomic_num2[40], i = 0, j = 0, m =0, status, status2;
int k =0;
double weight[40], weight2[40];
char name[40][40], symbol[40][40], name2[40][40], symbol2[40][40];
while(status != EOF)
{
status = fscanf(inp1, "%d%s%s%lf", &atomic_num[i], name[i], symbol[i], &weight[i]);
i++;
}
i = i-1;
while(status2 != EOF)
{
status2 = fscanf(inp2, "%d%s%s%lf", &atomic_num2[m], name2[m], symbol2[m], &weight2[m]);
m++;
}
m = m-1;
for(k = 0; k < m; k++)
fprintf(outp,"%d %s %s %.2f\n", atomic_num2[k], name2[k], symbol2[k], weight2[k]);
for(j = 0; j < i; j++)
fprintf(outp,"%d %s %s %.2f\n", atomic_num[j], name[j], symbol[j], weight[j]);
}
chem1.txt
1 Hydrogen H 1.01
1 Hydrogen H 1.01
1 Hydrogen H 1.01
50 Tin Sn 118.69
84 Polonium Po 209
88 Radium Ra 226.03
chem2.txt
1 Hydrogen H 1.01
1 Hydrogen H 1.01
1 Hydrogen H 1.01
1 Hydrogen H 1.01
1 Hydrogen H 1.01
1 Hydrogen H 1.01
11 Sodium Na 22.99
20 Calcium Ca 40.08
84 Polonium Po 209.00
this is what the output should look like
1 Hydrogen H 1.01
1 Hydrogen H 1.01
1 Hydrogen H 1.01
1 Hydrogen H 1.01
1 Hydrogen H 1.01
1 Hydrogen H 1.01
11 Sodium Na 22.99
20 Calcium Ca 40.08
50 Tin Sn 118.69
84 Polonium Po 209.00
88 Radium Ra 226.03
thanks,