Search code examples
cfilecompareargvargc

Comparing two files in c


As the title says I need to compare 2 files.If a line comes by ,that isnt the same between this 2 files,print that line (From both files).The code is giving a don't send error.I can't seem to be able to find the error in my code.

#include <stdio.h>
#include <string.h>
#define MAX 1000

int main(int argc, char *argv[]) {
    char c1, c2;
    char s1[MAX], s2[MAX];
    char *p1;
    char *p2;
    FILE *fp1;
    FILE *fp2;
    fp1 = fopen(argv[1], "r");
    fp2 = fopen(argv[2], "r");
    p1 = s1;
    p2 = s2;
    if (argc != 3) {
        printf("Wrong use of program \n ");
        return -1;
    }
    if (fp1 == NULL || fp2 == NULL) {
        printf("One or both of the files can't be used \n ");
        return -1;
    }
    while ((c1 = getc(fp1)) != EOF || (c2 = getc(fp2)) != EOF) {
        *p1 = c1;
        *p2 = c2;
        p1++;
        p2++;

        for (c1 = getc(fp1); c1 != '\n'; p1++) {
            *p1 = c1;
        }
        *p1 = '\0';

        for (c2 = getc(fp2); c2 != '\n'; p2++) {
            *p2 = c2;
        }
        *p2 = '\0';
        if (!(strcmp(s1, s2))) {
            printf("%s \n ", s1);
            printf("%s \n ", s2);
            return 0;
        }
    }
    return 0;
}

Solution

  • On the two for loops you have the getc on the initialisation part of the loop, it will put a character in c1 and c2 once and then do the loop until you go beyond the limit of s1 and s2 through your p1 and p2 pointers. Since the c1 != '\n' is never met unless your first character in the line is a '\n' it will probably throw a segmentation fault error.

    On the while, at least with gcc and on my system, it does lazy evaluation (EDIT: Not lazy but short-circuit evaluation, see comments) and the c2 = getc(fp2) part is not executed if the other part was true.

    You also don't reset p1 and p2 after each line.

    strcmp returns 0 if both strings are equal, and in C, 0 is false and non-zero is true, so you are exiting on the first match.

    Here is a barely functioning version of your code, you still need to work on it and take into account cases like one file being shorter than the other, one line in a file being bigger than 1000 characters (as it is right now it would overflow s1 or s2), etc.

    #include <stdio.h>
    #include <string.h>
    #define MAX 1000
    
    int main(int argc, char *argv[]) {
      char c1, c2;
      char s1[MAX], s2[MAX];
      char *p1;
      char *p2;
      FILE *fp1;
      FILE *fp2;
      p1 = s1;
      p2 = s2;
      if (argc != 3) {
        printf("Wrong use of program \n ");
        return -1;
      }
      fp1 = fopen(argv[1], "r");
      fp2 = fopen(argv[2], "r");
      if (fp1 == NULL || fp2 == NULL) {
        printf("One or both of the files can't be used \n ");
        return -1;
      }
      c1 = getc(fp1);
      c2 = getc(fp2);
      while ((c1 != EOF) && (c2 != EOF)) {
        for (; c1 != '\n'; p1++) {
            *p1 = c1;
            c1 = getc(fp1);
        }
        *p1 = '\0';
    
        for (; c2 != '\n'; p2++) {
            *p2 = c2;
            c2 = getc(fp2);
        }
        *p2 = '\0';
        if ((strcmp(s1, s2)) != 0) {
            printf("%s\n", s1);
            printf("%s\n", s2);
            return 0;
        }
        c1 = getc(fp1);
        c2 = getc(fp2);
        p1 = s1;
        p2 = s2;
      }
      if (c1 != EOF || c2 != EOF)
        printf("One of the files ended prematurely\n");
      return 0;
    }