-
Combine 3 files in C
I am beginer in C programing and need some help. I have to combine 3 files.
file1.txt:
1 Gregor Schlierenzauer
2 Martin Koch
3 Janne Happonen
4 Robert Kranjec
5 Janne Ahonen
file2.txt:
1 197.7
4 193.3
2 187.8
3 186.6
5 186
file3.txt
5 218.7
2 195.6
4 190.1
1 182.5
3 181
Output should be:
1 Janne Ahonen 404.7
2 Robert Kranjec 383.4
3 Martin Koch 382.6
4 Gregor Schlierenzauer 380.2
5 Janne Happonen 367.6
Thank you for any help!
-
Well, it's quite simple.
Create a struct that stores the names and each value from the files, as well as the ID of that entry. Create an array of them. (Hopefully using memory allocation). Could even use a linked list in here if you wanted to.
Take the strings from the first file, store those.
Read in the numbers from the other files and store them in the struct array.
Now, output the strings
You'll be looking for fopen, fclose, fgets, fscanf, fputs, and fprintf.
Hope this gives you an idea on what to do.
-
So far I have this:
Code:
#include <stdio.h>
int main(int argc, char *args[]){
FILE *smucarji, *serija1, *serija2;
char tekmovalci[50];
double rezultat1[10], rezultat2[10];
int index;
char index1[100], index2[100];
double tocke;
char tocke1[100], tocke2[100];
smucarji = fopen("smucarji.txt", "r");
if((smucarji != NULL)){
while(!feof(smucarji)){
for(int i = 0; i < 10; i++){
fscanf(smucarji, "%d", &index); //prebere startno številko
fgets(tekmovalci, 50, smucarji); // prebere ime in priimek
}
}
}
fclose(smucarji);
serija1 = fopen("serija1.txt", "r");
if((serija1 != NULL)){
while(!feof(serija1)){
for(int i = 0; i < 10; i++){
fscanf (serija1, "%s %s", index1, tocke1);
index = atoi(index1);
tocke = atof(tocke1);
rezultat1[index-1] = tocke; //poz-1 je glavna stvar da ti da k pravilnemu skakalcu
}
}
}
fclose(serija1);
serija2 = fopen("serija2.txt", "r");
if((serija2 != NULL)){
while(!feof(serija2)){
for(int i = 0; i < 10; i++){
fscanf (serija2, "%s %s", index2, tocke2);
index = atoi(index2);
tocke = atof(tocke2);
rezultat2[index-1] = tocke; //poz-1 je glavna stvar da ti da k pravilnemu skakalcu
}
}
}
fclose(serija2);
return 0;
}
Now I need printf. Help please