Thread: how to find coomon data in 3 files without rewind..

  1. #31
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am not allowed to use data structures
    i am allowed to have only the strings of one ow from each file

  2. #32
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i got a problem in your algorithm
    its copying 005 many times
    instead of once
    each time we update the smallest it
    puts in the file
    005 003 005 >>puts 005 in the file
    005 004 005 >>puts 005 in the file
    005 005 005 >>puts 005 in the file
    and my while condition doesnt work
    its not stoping in that point why??
    file1
    000
    001
    002
    003
    005

    file2
    003
    004
    005

    file3
    005
    006
    007



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define false 0
    #define true 1
    
    int equal(char*f1,char*f2,char*f3);
      int smallest(char*f1,char*f2,char*f3);
    int main()
    {
    
    	FILE *file[3];
    	FILE *common;
    	
    	char f[3][20];
    	 
    	int small;
        int equall;	 
    	file[0]=fopen("c:\\1.txt","r");
    	file[1]=fopen("c:\\2.txt","r");
    	file[2]=fopen("c:\\3.txt","r");
    	common=fopen("c:\\common.txt","w");
    	fscanf(file[0],"%9s",f[0]);
    	fscanf(file[1],"%9s",f[1]);
    	fscanf(file[2],"%9s",f[2]);
    	while(fgetc(file[0])!=EOF||fgetc(file[1])!=EOF||fgetc(file[2])!=EOF)
    	{
    	    small=smallest(f[0],f[1],f[2]);
    	    equall=equal(f[0],f[1],f[2]);
    		if(equall!=-1)
    		fputs(f[equall],common);
    		fputc('\n',common);
    		fscanf(file[small],"%20s",f[small]);
    	} 
    
    	fclose(file[1]);
    	fclose(file[2]);
    	fclose(file[0]);
    	return 0;
    }
    
    int smallest(char* f_a,char* f_b,char* f_c)
    {
         int i=0;
    	 char* pLowest = f_a;
         if(strcmp(pLowest, f_b) > 0)
    	 {	
    		 i=1;
          pLowest = f_b;
    	 }
          if(strcmp(pLowest, f_c) > 0)
    	  {
           pLowest = f_c;
    	  i=2;
    	  }
          return i;
    }
    
      int equal(char* f1,char*f2,char*f3)
      {
    	   
         if(strcmp(f1,f3)==0)
    	 {
           return  0;
    	 }
    	 if(strcmp(f1,f2)==0)
    	 {
           return  1;
    	 }
         if(strcmp(f2,f3)==0)
    	 {
           return  2;
    	 }
    	  return -1;
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing data in two files
    By nynicue in forum C Programming
    Replies: 25
    Last Post: 06-18-2009, 07:35 PM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  4. reading input files with different types of data
    By sanu in forum C++ Programming
    Replies: 4
    Last Post: 06-27-2002, 08:15 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM