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

This is a discussion on how to find coomon data in 3 files without rewind.. within the C Programming forums, part of the General Programming Boards category; after the pointed comment i dont know from what file the smallest belongs to i dont know in what two ...

  1. #16
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    after the pointed comment
    i dont know from what file the smallest belongs to
    i dont know in what two files i got the equality
    so i could write it somewhere and go the the next line of the "smallest" file
    ??
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define false 0
    #define true 1
    
    char* equal(char*f1,char*f2,char*f3);
      char* smallest(char*f1,char*f2,char*f3);
    int main()
    {
    
    	FILE *file1,*file2,*file3;
    	
    	char f1[20];
    	char f2[20];
    	char f3[20];
    	char* small;
        char* equall;	 
    	file1=fopen("c:\\1.txt","r");
    	file2=fopen("c:\\2.txt","r");
    	file3=fopen("c:\\3.txt","r");
    	fscanf(file1,"%9s",f1);
    	fscanf(file2,"%9s",f2);
    	fscanf(file3,"%9s",f3);
    	 small=smallest(f1,f2,f3);
    	 equall=equal(f1,f2,f3); //how to continue after this point
    	                                      
    	fclose(file1);
    	fclose(file2);
    	fclose(file3);
    	return 0;
    }
    
    char* smallest(char*f1,char*f2,char*f3)
    {
    
    	 char* pLowest = f1;
         if(strcmp(pLowest, f2) > 0)
          pLowest = f2;
          if(strcmp(pLowest, f3) > 0)
           pLowest = f3;
          return pLowest;
    }
    
      char* equal(char*f1,char*f2,char*f3)
      {
         if(strcmp(f1,f3)==0)
    	 {
           return f1;
    	 }
         if(strcmp(f2,f3)==0)
    	 {
           return f2;
    	 }
    	  return NULL;
      }

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you need a function that actually tells you which file to read next, when you have three sets of SSN.

    And of course, you need to do a loop of some sort.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how to return the file in this function
    how to know what file is the smallest string
    ??
    Code:
    char* smallest_file(char*f1,char*f2,char*f3,FILE* file1,FILE* file2,FILE* file3)
    {
    
    	 char* pLowest = f1;
         if(strcmp(pLowest, f2) > 0)
          pLowest = f2;
          if(strcmp(pLowest, f3) > 0)
           pLowest = f3;
          return pLowest;
    }

  4. #19
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    is there any shorter way then
    Code:
    char* smallest_file(char*f1,char*f2,char*f3,FILE* file1,FILE* file2,FILE* file3)
    {
    
    	 char* pLowest = f1;
         if(strcmp(pLowest, f2) > 0)
          pLowest = f2;
          if(strcmp(pLowest, f3) > 0)
           pLowest = f3;
    	  if(strcmp(f1,pLowest)==0)
    	  {
    		  return file1;
    	  }
          if(strcmp(f2,pLowest)==0)
    	  {
    		  return file2;
    	  } 
    	  if(strcmp(f3,pLowest)==0)
    	  {
    		  return file3;
    	  }
    
    }

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, I suppose you could write a function that updates the lowest string(s) by reading from the relevant file, rather than making a function that returns the file wanted.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    you say to write a function that updates the lowest string(s) by reading from the relevant file.
    but i have to know from what file the lowest string came from.

    so i cant understand how i can manage doing what you told me
    without building such function?

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you keep the files and the strings in the same order at all times, you can determine if the value from file X needs updating, and read the file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    you say
    build some how that you could know which file to update each time

    but its too abstract
    what sub operations i need to build?

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Shouldn't need many sub-operations. Just string compare and fscanf() or something like that, and a few if-statements.

    I just hacked one up in about 13 lines of code, with 5 if-statements (including finding the lowest one).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #25
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok here is the comparing part
    Code:
    char* smallest(char*f1,char*f2,char*f3,FILE* file1,FILE* file2,FILE* file3)
    {
    
    	 char* pLowest = f1;
         if(strcmp(pLowest, f2) > 0)
          pLowest = f2;
          if(strcmp(pLowest, f3) > 0)
           pLowest = f3;
          return pLowest;
    }
    here is the scanf part
    Code:
          fscanf(file1,"%9s",f1);
    	fscanf(file2,"%9s",f2);
    	fscanf(file3,"%9s",f3);
    for what reason i need to use
    "a few if-statements"

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need if-statments to determine if you need to read from the file or not (if you already have a value higher than the lowest, you should not read the file).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok i tried to write it
    but how to know which string is the smallest and which file it belongs to
    without writing the previous function that i showed
    ??
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define false 0
    #define true 1
    
    char* equal(char*f1,char*f2,char*f3);
      char* smallest(char*f1,char*f2,char*f3);
    int main()
    {
    
    	FILE *file1,*file2,*file3;
    	
    	char f1[20];
    	char f2[20];
    	char f3[20];
    	char* small;
        char* equall;	 
    	file1=fopen("c:\\1.txt","r");
    	file2=fopen("c:\\2.txt","r");
    	file3=fopen("c:\\3.txt","r");
    
    	while(fgetc(f1)!=EOF)||(fgetc(f2)!=EOF)||(fgetc(f1)!=EOF))
    	{
    	   fscanf(file1,"%9s",f1);
    	   fscanf(file2,"%9s",f2);
    	   fscanf(file3,"%9s",f3);
    	   small=smallest(f1,f2,f3);
    
    	   equall=equal(f1,f2,f3);
    	} 
    	fclose(file1);
    	fclose(file2);
    	fclose(file3);
    	return 0;
    }
    
    char* smallest(char*f1,char*f2,char*f3,FILE* file1,FILE* file2,FILE* file3)
    {
    
    	 char* pLowest = f1;
         if(strcmp(pLowest, f2) > 0)
          pLowest = f2;
          if(strcmp(pLowest, f3) > 0)
           pLowest = f3;
          return pLowest;
    }
    
       char* equal(char*f1,char*f2,char*f3)
      {
         if(strcmp(f1,f3)==0)
    	 {
           return f1;
    	 }
         if(strcmp(f2,f3)==0)
    	 {
           return f2;
    	 }
    	  return NULL;
      }

  13. #28
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how to continue??

  14. #29
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how to combine those parts

  15. #30
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    How about using a dynamically allocated array of structs whose elements are: SSN, current file number and a count. SSNs whose count equals 3 are present in all of the files. Just my 2c.

Page 2 of 3 FirstFirst 123 LastLast
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, 08:35 PM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 07:43 AM
  3. Replies: 26
    Last Post: 06-15-2005, 03:38 PM
  4. reading input files with different types of data
    By sanu in forum C++ Programming
    Replies: 4
    Last Post: 06-27-2002, 09:15 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

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