Dear,
I'm trying write a simple C program that h two input files. Both are large files. First file has one column and second file has two columns. Just compare the first colums of both files and print the whole line of second file that matches first one.

File2.txt

123456 ABCDE
234567 DEFER
563467 UIERT
349056 UIFRT

File1.txt

234567
563467

The output file should be like this

234567 DEFER
563467 UIERT

Could some one help me in finding logic.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
FILE *f1;
FILE *f2;
FILE *f3;
void compare(char filename1[30],char filename2[30]){
  f1=fopen(filename1,"r"); 
  f2=fopen(filename2,"r"); 
  f3=fopen("new.txt","w");
  char word1[40];
  char word2[40];
  int k=0;
  while(!feof(f1)){
           fscanf(f1,"%s",word1);
           if(k==1){
                    fprintf(f3,"%s\n",word1);
                    k=0;
                    }   
           fseek(f2,0,0);      
        while(!feof(f2)){
          fscanf(f2,"%s",word2);

          if(strcmp(word1,word2)==0){

            k++;                         
           fprintf(f3,"%s\t",word1);
           break;
           }
           }
           }
           }

main(){
       compare("f1.txt","f2.txt");

       }
Comments
Don't do people's homework assignments for them...
no

Use pseudo-code to describe the steps you need to take to accomplish your end. Then, use that to write the code. Don't ask us to solve your homework problems for you. We will help you when you run into a problem you cannot solve, but YOU have to make the effort!

This article has been dead for over six months. Start a new discussion instead.