My first quistion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adelkhamis
    New Member
    • Feb 2010
    • 1

    My first quistion

    Hi every body..
    I've corrected my code in which I would like to compare strings of two txt files;

    file1.txt=
    054,8100,171
    054,8100,191


    file2.txt=
    054,8100,171
    054,8100,191
    054,8100,171
    054,8100,181
    054,8100,171
    054,8100,171
    054,8100,171

    to be as following:

    Code:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<conio.h>
    
    void pError(char * s){
       printf("%s\n",s);
    }
    
    int main(){
    
    int equal_lines=0;
    FILE *pt1,*pt2;
    pt1=fopen("file1.txt","r");
    if (pt1==NULL)
       pError("open error f1");
    pt2=fopen("file2.txt","r");
    if (pt2==NULL)
       pError("open error f2");
    
    short q=0;
    char c1[50],c2[50]; //declaring a string to hold the content of each file.
    int buf_size=sizeof(c1);
    
    fgets(c1,buf_size,pt1);
    while(((fgets(c2,buf_size,pt2))!= NULL)&&(!q))
       {
            printf("f1: %s\n",c1);
            printf("f2: %s\n",c2);
    
    
             if(strcmp(c1,c2)==0)
               equal_lines++;
    
    
       }
    
    printf("\n\n equal_lines=%d",equal_lines);
    getch();
    return 0;
    }
    untill this point, it's perfectly achieve comparing 1st string of file1 with all strings of file2...
    but know, I want to compare the 2nd string of file1 with all string of file2.. and so on consecutively..

    I would greatly appreciate it if I could get some assistance! Thanks in advance!
    Last edited by Banfa; Feb 22 '10, 10:30 PM. Reason: Added [code] ... [/code] tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Just before/at/instead of line 25 you need a loop similar to line 26 that operates on file 1.

    Comment

    Working...