Combine 3 files in C

This is a discussion on Combine 3 files in C within the C Programming forums, part of the General Programming Boards category; I am beginer in C programing and need some help. I have to combine 3 files. file1.txt: 1 Gregor Schlierenzauer ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    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!

  2. #2
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    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.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Unhappy

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21