// 1.FileIO: Search common words.

#include<stdlib.h>
#include<stdio.h>
#include<strings.h>

#define MAX_WL 256

int SearchFile(FILE *f, char *str) {
  char s[MAX_WL];
  do{
    if(fscanf(f,"%s",s))
      if(!strcmp(str,s)) return 1; // Same word found.
  } while(!feof(f));
  clearerr(f);
  rewind(f);      
}

main(int argc, char *argv[]) {
  FILE *f1,*f2,*fTmp; char str[ MAX_WL]; unsigned int count;
  if(argc!=3) { printf("usage: a.out file1 file2\n"); exit(1); }
  if(!(f1=fopen(argv[1],"r"))) { printf("open in-file1 error!\n"); exit(1); }
  if(!(f2=fopen(argv[2],"r"))) { printf("open in-file2 error!\n"); exit(1); }
  count=0;
  do{
    if(fscanf(f1,"%s",str))
      //if(fgets(str,MAX_WL,f1))
      if(SearchFile(f2,str)) { 
	count++;
	//printf("%d found: %s\n",count,str);
        printf("%s\n",str);
      }
  } while(!feof(f1));
  close(f1); close(f2);
}

