user2264035
user2264035

Reputation: 595

How to Write Differences in Two Text Files to Another Text File in C?

I'd like to write a program that compares two files and writes every byte in file one that is different from file two into a third file. I want to compare the files byte by byte and write any differing single bytes to the third file. I'm not very familiar with file I/O. Can someone give me an example program that accomplishes this task?

This is what I have so far:

int main(int argc, char *argv[]) {
  int file1, file2, file1size, file2size;
  // int  difference1, difference2;
  char buf;

  if (argc != 3){
    fprintf(stderr, "Usage %s <file1> <file2>", argv[0]);
    exit(1);
  }
  if ((file1 = open(argv[1], 0400)) < 0) { //read permission for user on file source
    fprintf(stderr, "Can't open source");
    exit(1);
  }
  if ((file2 = open(argv[2], 0400)) < 0) { //read permission for user on file source
    fprintf(stderr, "Can't open source");
    exit(1);
  }
  file1size = lseek(file1, (off_t) 0, SEEK_END);
  printf("File 1's size is %d\n", file1size);
  file2size = lseek(file2, (off_t) 0, SEEK_END);
  printf("File 2's size is %d\n", file2size);



}

I'm not sure how to compare file1 and file2's bytes and then write the differences to another file.

Upvotes: 1

Views: 626

Answers (2)

ChuckCottrill
ChuckCottrill

Reputation: 4444

This does what you want, compiles, and runs,

#include <stdio.h>
int main(int argc, char *argv[])
{
    int offset;
    int argi=1;
    int ch1, ch2;
    FILE *fh1, *fh2, *fh3=stdout;
        FILE *fh4=stdout;
    if( argc<3 ) {
        printf("usage: diff <file> <file> { <outfile> }\n"); return(1);
    }
    if(argi<argc) {
        if(!(fh1 = fopen(argv[argi], "r"))) {
            printf("cannot open %s\n",argv[argi]); return(2);
        }
    }
    if(++argi<argc) {
        if(!(fh2 = fopen(argv[argi], "r"))) {
            printf("cannot open %s\n",argv[argi]); return(3);
        }
    }
    if(++argi<argc) {
        if(!(fh3 = fopen(argv[argi], "w+"))) {
            printf("cannot open %s\n",argv[argi]); return(4);
        }
    }
        if(++argi<argc) {
            //duplicate output to a second file?
            if(!(fh4 = fopen(argv[argi], "r"))) {
                printf("cannot open %s\n",argv[argi]); return(3);
            }
        }
    for(offset = 0; (!feof(fh1)) && (!feof(fh2)); offset++)
    {
        ch1=ch2='-';
        if(!feof(fh1)) ch1 = getc(fh1);
        if(!feof(fh2)) ch2 = getc(fh2);
        if(ch1 != ch2) {
            fprintf(fh3,"%d:%c %c\n", offset, ch1, ch2);
            //additional file here
        }
        else {
            fprintf(fh3,"%c\n", ch1);
            //additional file here
        }
    }
    return 0;
}

More typically, you would read entire lines using fgets, and strcmp to compare the lines. Here is how,

    char str1[1024], str2[1024];
    ...
    for(offset = 0; (!feof(fh1)) && (!feof(fh2)); offset++)
    {
        strcpy(str1,"-");strcpy(str2,"-");
        if(!feof(fh1)) fgets(str1,sizeof(str1),fh1);
        if(!feof(fh2)) fgets(str2,sizeof(str1),fh2);
        if(strcmp(str1,str2)!=0) 
            fprintf(fh3,"%d:%s  %s", offset, str1, str2);
        else
            fprintf(fh3,"%c", str1);
    }

Upvotes: 2

Paul Draper
Paul Draper

Reputation: 83323

This is close to what you are looking for.

#include <stdio.h>

int main(int argc, char *argv[]) {
    FILE *file1 = fopen(argv[1], "r");
    FILE *file2 = fopen(argv[2], "r");
    int i;
    for(i = 0; !feof(file1) || !feof(file2); i++) {
        int byte1 = getc(file1);
        int byte2 = getc(file2);
        if(byte1 != byte2) {
            printf("%d %d %d\n", i, byte1, byte2);
        }
    }
    return 0;
}

It takes the two files as command line arguments and compares the two byte-by-byte. If two bytes are different, it printf the character #, and the ASCII values of the two characters. -1 means EOF was already reached.

You'll have to (understand and) adapt this to the output format you want. (I'm assuming this is homework.)

feof tests for end-of-file.

getc gets the next character (byte) from the file. It is -1 if the end of the file has been reached.

And you seem already to know what printf does.

Upvotes: 2

Related Questions