Solved

compare 2 files?

Posted on 2004-04-28
24
295 Views
Last Modified: 2010-04-02
I'm trying to compare the values in two different input files and based on the matching of a certain value I output it to another file.
If my file1 has 10 rows and my file2 has 5 rows I want the first row of file1 to compare with all the rows in file 2 and then go to the 2nd row of file 1 and compare with all the rows in file 2 and so on.. .
The code I have written does not do exactly what I want. Can someone correct my while and if loops.
Thanks
ps: my tokenize function works well!

ifp1 = fopen("c:/input1.txt", "r");
ifp2 = fopen("c:/input2.txt", "r");
ffp = fopen("c:/out.txt","w");


while(fgets( linein1, sizeof( linein1 ), ifp1 ) != NULL)
{
TokenCount1 = tokenize1( linein1, ",\n", tokens, X_TOKEN1 );

if(fgets( linein2, sizeof( linein2 ), ifp2 ) != NULL)

TokenCount2 = tokenize2( linein2, ",\n", token, X_TOKEN2 );

if (( TokenCount1 == 21 ) && (TokenCount2 == 5))
{
if(strcmp(tokens[7],token[0])==0)
{
fprintf(ffp, "Service Date ..........<%s>\n",tokens[7]);
}
}
}
return( 0 );
0
Comment
Question by:bjvar
  • 13
  • 11
24 Comments
 
LVL 2

Expert Comment

by:Avik77
ID: 10942204
Can u plz give ur tokenize1 and tokenize2 functions
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10942242
>>if(fgets( linein2, sizeof( linein2 ), ifp2 ) != NULL)
change it to :
while(fgets( linein2, sizeof( linein2 ), ifp2 ) != NULL)
because it must compare all the records of ur second file

Avik.
0
 
LVL 2

Accepted Solution

by:
Avik77 earned 125 total points
ID: 10942320
ur code may finally stand like this...

ifp1 = fopen("c:/input1.txt", "r");
ffp = fopen("c:/out.txt","w");

while(fgets( linein1, sizeof( linein1 ), ifp1 ) != NULL)
{
TokenCount1 = tokenize1( linein1, ",\n", tokens, X_TOKEN1 );
ifp2=fopen("c:/input2.txt", "r"); // Assuming it exists

while(fgets( linein2, sizeof( linein2 ), ifp2 ) != NULL) {

TokenCount2 = tokenize2( linein2, ",\n", token, X_TOKEN2 );

if (( TokenCount1 == 21 ) && (TokenCount2 == 5))
{
if(strcmp(tokens[7],token[0])==0)
  fprintf(ffp, "Service Date ..........<%s>\n",tokens[7]);
}
}
fclose(ifp2);
}
return( 0 );

Avik.
0
 

Author Comment

by:bjvar
ID: 10942515
Thanks Avik. But when I do the change that you made, it compares all the rows of file1 with the last row of file2 and nothing else! Please advise
My tokenize is a simple function which works.

int tokenize1( char *line, char *separators, char *tokens[], int max )
{
   int i = 0;
   char *pch;
   
   pch = strtok( line, separators );  /* get 1st token addr */
   while( pch != NULL )               /* repeat for each token */
   {
      if ( i < max )
      {
         tokens[i] = pch;    /* save token address in array */
      }
      pch = strtok( NULL, separators );  /* next token address */
      i++;
   }
   return( i );
}
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10942620
Did u put the {'s and }'s  correctly after the second while.
Check it !
Avik.
0
 

Author Comment

by:bjvar
ID: 10942733
Avik.. You are a genius! I think its working but I do all my test cases and see if its working well.. If I have more questions, I'll reply
Thanks again!
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10947658
If ur input file sizes are not too long, u could have saved all the lines in two seperate array of strings (char pointers) and comparson to have done betwwen the 2D buffers.

while(fgets(filein1,sizeof(filein2),ifp1)!=NULL){strcpy(fil1[j++],filein1);}
while(fgets(filein2,sizeof(filein2),ifp1)!=NULL){strcpy(fil1[j++],filein2);}
// where fil1 and fil2 are array of char pointers.

u can dynamically specify the size of arrays by checking the number of lines in each of the files or assign an arbitrary large value which could waste ur space.Then comparison would be carried out between buffers.
This is to avoid repeated openning and closing of disk files which can slow ur application.

Avik.
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10947773
sorry, next while fgets hasn ifp2 instead of ifp1 and fil2[j++] instead of fil1..Rather many mistakes . get it...
while(fgets(filein1,sizeof(filein1),ifp1)!=NULL){strcpy(fil1[j++],filein1);}
j=0;fclose(ifp1);
while(fgets(filein2,sizeof(filein2),ifp2)!=NULL){strcpy(fil2[j++],filein2);}
fclose(ifp2);

Avik.
0
 

Author Comment

by:bjvar
ID: 10980011
I want to compare string1 to another string2. But if string 2 is a blank field I need to do something. How do I compare a string to a blank field?
Thanks
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10980413
Suppose string2 is blank....blank means no character..i.e. ""
Hence u can do a check if u suspect string2 to be a blank field by ...

if(strcmp(string2,"")==0){/* take action as string2 is blank */}
the strcmp function is present in string.h

Hope this helps u.

Avik.
0
 

Author Comment

by:bjvar
ID: 10980510
Thanks for helping Avik.
I hope I can better explain my problem:

The length of the string2 need not be 0. String2 could have 15 spaces allocated for it but in the file from which I read string2 erroneously the value is not in the file.
So if that field is empty an error message should pop up.


0
 
LVL 2

Expert Comment

by:Avik77
ID: 10980619
I still need a proper explanation for this....
>>I read string2 erroneously the value is not in the file.
Is it so that u have a value in the string and u r searching for that value in the line read from the file.....but it is not present there....use strstr...
if(strstr(line,string2)==NULL){/* no occurance of string2 in line */}

Please respond fast as I won't be here too long...
Avik.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:bjvar
ID: 10980742
I'll try it out today and will let you know tomorrow. Have a good day and thanks!
0
 

Author Comment

by:bjvar
ID: 10987489
Sorry for all the trouble but let me try to first fix another problem I just encountered.
All this while, the input file that I had was delimited by a comma but now I have a file were the fields are fixed-width and I need to tokenize it.(Even if it is a blank space between two fields).

while( fgets( linein, sizeof( linein ), ifp ) != NULL )
   {TokenCount = tokenize( linein, ",\n", tokens, MAX_TOKEN );
   }

int tokenize( char *line, char *separators, char *tokens[], int max )
{
   int i = 0;
   char *pch;
   
   pch = strtok( line, separators );  /* get 1st token addr */
   while( pch != NULL )               /* repeat for each token */
   {
      if ( i < max )
      {
        tokens[i] = pch;    /* save token address in array */
      }
      pch = strtok( NULL, separators );  /* next token address */
      i++;
   }
   return( i );
}
0
 

Author Comment

by:bjvar
ID: 10988931
I solved the above problem! Yippee!! Thanks. So discard the last question
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10989007
Did u get ur first problem fixed...???
0
 

Author Comment

by:bjvar
ID: 10989393
I dont think so. I still have to explain it to you in a better way to which I will shortly.
0
 

Author Comment

by:bjvar
ID: 10996638
The more I work on this program the more doubts I have.
I am having a problem with if-else and while loop. Can you please tell me what I'm doing wrong.
I have 3 input files ifp1, ifp2, ifp3

 while(fgets( linein1, sizeof( linein1 ), ifp1 ) != NULL)
   {
   TokenCount1 = tokenize1( linein1, ",\n", tokens, MAX_TOKEN1 );

   ifp2 = fopen("c:/input.txt", "r");
   while(fgets( linein2, sizeof( linein2 ), ifp2 ) != NULL)
   {
   TokenCount2 = tokenize2( linein2, " ", token, MAX_TOKEN2 );

   ifp3 = fopen("c:/input3.txt","r");
   while(fgets(linein3, sizeof(linein3), ifp3) != NULL)
    TokenCount3 = tokenize3( linein3, ",\n", token3, MAX_TOKEN3 );
   {
      if (Condition)
  {
    fprintf(ofp1,"%s",tokens[0]);
   }
     else
      fprintf(ofp2,"%20s",token[0]);
   }  
   fclose(ifp3);
   }
   fclose(ifp1);
  }
   return(0);
   fclose(ifp2);
}
0
 

Author Comment

by:bjvar
ID: 10997200
what I'm trying to do in the previous question is if th condition is satified then output it to fp1 else if not output in fp1 put in fp2.
Hope it is a little more clearer
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10998947
Then what is ifp3 for ???
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10999002
Moreover where did u open the files in write mode ?? i.e ofp1 , ofp2
 while(fgets(linein3, sizeof(linein3), ifp3) != NULL)
    TokenCount3 = tokenize3( linein3, ",\n", token3, MAX_TOKEN3 );
   {   // WHAT IS THIS FOR
      if (Condition)
  {
    fprintf(ofp1,"%s",tokens[0]);   // DID U OPEN IT ??
   }
     else
      fprintf(ofp2,"%20s",token[0]); // OR THIS IN WRITE MODE ??
   }  

there are still things to cleared upon , if u don't mind please give the complete statement of ur problem (if not too much complex) . I won't do it because it is illegal here in EE, but I can try to pinpoint some mistakes still there. U can also open a new question for this to allow other experts try this as I am only available for a certain time slot here...Bye and best of luck.

Avik.
0
 

Author Comment

by:bjvar
ID: 10999035
suppose ifp1 has rows of data with different dates and Account nos.
ifp2 has rows with 1 date but different account nos.
if ((date(ifp1)==date(ifp2)&&( acc(ifp1)==acc(ifp2))
{ then put in  ofp1}
if ((date(ifp1)==date(ifp2)
{put all the other documents from ifp1 which are not in ofp1 to ofp2}
else
put all the rest of the rows not processed into ofp3 which will in the next stage become ifp3.

I hope you understand. You have been a great help to me! I'm just learning to use C.

0
 

Author Comment

by:bjvar
ID: 10999163
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int tokenize1( char *line, char *separators, char *tokens[], int max );
int tokenize2( char *line2, char *separators2, char *tokens2[], int max2 );
int tokenize3( char *line3, char *separators3, char *tokens3[], int max3 );
void deblank( char *b );

int main ()  
{
   FILE *ifp1, *ifp2, *ifp3, *ofp1,*ofp2, *ffp;
        
   #define MAX_TOKEN1 23
   #define MAX_TOKEN2 6
   #define MAX_TOKEN3 23
   char *tokens[MAX_TOKEN1];
   char *token[MAX_TOKEN2];
   char *token3[MAX_TOKEN3];
   char *str, *str2;
   char linein1[250];
   char linein2[250];
   char linein3[250];
   int  TokenCount1,TokenCount2,TokenCount3;
   char *number[15]= {{"256 "},{"257 "},{"258 "},{"259 "},{"C"},{"270 "},{"271 "},
            {"272 "},{"273 "}, {"274 "},{"275 "},{"276 "},{"277 "},{"278 "},{"279 "}};

       ifp1 = fopen("c:/input1.txt", "r");
       ofp1 = fopen("c:/out1.txt","w");
       ofp2 = fopen("c:/out2.txt","w");
       ofp3  = fopen("c:/out.txt","w");

       fprintf(ofp1, "KEANE \n");
       fprintf(ofp1,"\n");
       fprintf(ofp2, "PARAGON \n");
       fprintf(ofp2,"\n");
       fprintf(ffp, "UNPROCESSED_DATA \n");
       fprintf(ffp,"\n");

      while(fgets( linein1, sizeof( linein1 ), ifp1 ) != NULL)
        {
        TokenCount1 = tokenize1( linein1, ",\n", tokens, MAX_TOKEN1 );
        ifp2 = fopen("c:/input2.txt", "r");      
            
        while(fgets( linein2, sizeof( linein2 ), ifp2 ) != NULL)
        {

        TokenCount2 = tokenize2( linein2, " ", token, MAX_TOKEN2 );
        ifp3 = fopen("c:/input3.txt","r");
                  
        while(fgets(linein3, sizeof(linein3), ifp3) != NULL)
              TokenCount3 = tokenize3( linein3, ",\n", token3, MAX_TOKEN3 );
        {

      if (( TokenCount1 == 23 ) && (TokenCount2 == 6))
        {
              if((strcmp(tokens[7],token[0])==0) &&(strcmp(tokens[3],token[1])==0))
            {
                    
            if((strcmp(tokens[21],number[3])==0)|| (strcmp(tokens[21],number[2])==0) \
              || (strcmp(tokens[21],number[1])==0) ||(strcmp(token[21],number[0])==0))\
              /* seeing if NRV = 256-259)*/
              { tokens[4]="000016001"; }/* chg all the chrg code to 16001 if nrv=256-259*/
      
            fprintf(ofp1,"%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n",tokens[0],\
             tokens[1],tokens[2],tokens[3],tokens[4],tokens[5],tokens[6],tokens[8],\
             tokens[9],tokens[10],tokens[15]);
            }  
             

            if ((strcmp(tokens[7],token[0])==0) &&(strcmp(tokens[3],token[1])==0)&&(strcmp(tokens[21],number[3])!=0)\
              && (strcmp(tokens[21],number[2])!=0) && (strcmp(tokens[21],number[1])!=0) && \
              (strcmp(token[21],number[0])!=0))
            {
                  if (strcmp(tokens[12],number[4])==0)
                tokens[12]="02";
            else tokens[12]="01";
              fprintf(ofp2,"%4s %10s %10s %8s %40s %20s %10s %20s %20s\n",tokens[0],,tokens[0],tokens[0],tokens[7],tokens[0],\
                   tokens[0],tokens[6],tokens[3],tokens[0]);
            }
        }
        }
        fclose(ifp3);
        }
        fclose(ifp2);
       }
   return(0);
                 
   fclose(ifp1);
   fclose(ofp3);
   fclose(ofp1);
   fclose(ofp2);
}


/* break input into tokens and save addresses in arrays */
int tokenize1( char *line, char *separators, char *tokens[], int max )
{
   int i = 0;
   char *pch;
   
   pch = strtok( line, separators );  /* get 1st token addr */
   while( pch != NULL )               /* repeat for each token */
   {
      if ( i < max )
      {
       //deblank( pch );     /* strip leading, extra and trailing spaces */
         tokens[i] = pch;    /* save token address in array */
      }
      pch = strtok( NULL, separators );  /* next token address */
      i++;
   }
   return( i );
}

int tokenize2( char *line2, char *separators2, char *tokens2[], int max2 )
{
   int i = 0;
   char *pch;
   
   pch = strtok( line2," ");  /* get 1st token addr */
   while( pch != NULL )               /* repeat for each token */
   {
      if ( i < max2 )
      {
      // deblank( pch );     /* strip leading, extra and trailing spaces */
         tokens2[i] = pch;    /* save token address in array */
      }
      pch = strtok( NULL," ");  /* next token address */
      i++;
   }
   return( i );
}

int tokenize3( char *line3, char *separators3, char *tokens3[], int max3 )
{
   int i = 0;
   char *pch;
   
   pch = strtok( line3, separators3 );  /* get 1st token addr */
   while( pch != NULL )               /* repeat for each token */
   {
      if ( i < max3 )
      {
      // deblank( pch );     /* strip leading, extra and trailing spaces */
         tokens3[i] = pch;    /* save token address in array */
      }
      pch = strtok( NULL, separators3 );  /* next token address */
      i++;
   }
   return( i );
}

I havent done anything with ifp3 yet. So no need to look into that if you dont undertstand.
 thanks for all your help. Else I'll start a new question like you said.
Thanks again. You have been very patient!
0
 

Author Comment

by:bjvar
ID: 11006186
Thanks Avik, a lot! I'll try to explain it once more before I pay money and buy more points. Even if I do I would like to give the points to you.

I made most of what I want to do to work. except that because of the while loops it prints out twice in the output. Is there any way I can correct that? Please let me know. Ignore ifp3

       ifp1 = fopen("c:/input1.txt", "r");
       if (ifp1 == NULL) {
        fprintf(stderr, "Error opening file ifp1(printed to standard error)\n");
        exit (1);
        }
       ofp1 = fopen("c:/out1.txt","w");
       ofp2 = fopen("c:/out2.txt","w");
       ffp  = fopen("c:/out.txt","w");

       fprintf(ofp1, "KEANE \n");
       fprintf(ofp1,"\n");
       fprintf(ofp2, "PARAGON \n");
       fprintf(ofp2,"\n");
       fprintf(ffp, "UNPROCESSED_DATA \n");
       fprintf(ffp,"\n");


      while(fgets( linein1, sizeof( linein1 ), ifp1 ) != NULL)
        {
        TokenCount1 = tokenize1( linein1, ",\n", tokens, MAX_TOKEN1 );
        str2 = token[14];
        ifp2 = fopen("c:/input2.txt", "r");      
        if (ifp2 == NULL) {
        fprintf(stderr, "Error opening file ifp2(printed to standard error)\n");
        exit (1);
        }
        while(fgets( linein2, sizeof( linein2 ), ifp2 ) != NULL)
        {
      TokenCount2 = tokenize2( linein2, " ", token, MAX_TOKEN2 );
       str1 = token[0];
        
      if (( TokenCount1 == 23 ) && (TokenCount2 == 6))
        {
             if(strcmp(tokens[7],token[0])==0)
              {
                    if(strcmp(tokens[3],token[1])==0)
                        {                   
            if((strcmp(tokens[21],number[3])==0)|| (strcmp(tokens[21],number[2])==0) \
              || (strcmp(tokens[21],number[1])==0) ||(strcmp(token[21],number[0])==0))\
              /* seeing if NRV = 256-259)*/
              { tokens[4]="000016001"; }/* chg all the chrg code to 16001 if nrv=256-259*/
      
            fprintf(ofp1%s,%s,%s,%s\n",tokens[19],tokens[0],\
             tokens[1],tokens[2],tokens[3],tokens[4]);
                        }  
             
            }//end if date
      
             else fprintf( ffp, "%s\n",tokens[19]);// EACH DATA PRINTS OUT TWICE HERE
        
        }        //end if
    if ((TokenCount1 == 23) &&(strcmp(tokens[7],str1)==0) &&(strcmp(tokens[21],number[3])!=0)\
              && (strcmp(tokens[21],number[2])!=0) && (strcmp(tokens[21],number[1])!=0) && \
              (strcmp(token[21],number[0])!=0))
            {
            if (strcmp(tokens[12],number[4])==0)
            tokens[12]="02";
                                else tokens[12]="01";
              fprintf(ofp2,"%s \n",tokens[19],
            } //EACH DATA PRINTS OUT TWICE HERE
        }//end while ifp2
        fclose(ifp2);
       }

   return(0);
                 
   fclose(ifp1);
   fclose(ffp);
   fclose(ofp1);
   fclose(ofp2);
}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now