简体   繁体   中英

How to read two text files into two dynamically allocated arrays and compare them byte by byte in c?

I'd like to write a program that compares two text files and writes every byte in file two that is different from file one into a third file. The catch is that the text files must be read into dynamically allocated arrays. The arrays are compared byte by byte and any byte in file two's array that is different than file one's array will be put into a third array. Then copy that array into a new text file. How would this be accomplished?

Basically how would the following code get the same result using dynamically allocated arrays?

#include <stdio.h>
int main(int argc, char *argv[])
{
    int offset;
    int ch1, ch2;
    FILE *fh1, *fh2, *fh3=stdout;
    if( argc<3 ) {
        printf("need two file names\n"); return(1);
    }
    if(!(fh1 = fopen(argv[1], "r"))) {
        printf("cannot open %s\n",argv[1]); return(2);
    }
    if(!(fh2 = fopen(argv[2], "r"))) {
        printf("cannot open %s\n",argv[2]); return(3);
    }
    if(argc>3) {
        if(!(fh3 = fopen(argv[3], "w+"))) {
            printf("cannot open %s\n",argv[3]); return(4);
        }
    }
    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);
        else
            fprintf(fh3,"%c\n", ch1);
    }
    return 0;
}

Trivial approach might be using fseek() + ftell() to determine size of file and allocate space then. Another way can be

size_t pos = 0;
size_t allocated = 1024;  /* arbitrary value */
void *tmp = malloc(allocated);
BUG_ON(!tmp)
while (!feof(f)) {
    size_t l = fread(tmp + pos, 1, allocated - pos, f);
    pos += l;
    allocated += l + 1024;    /* arbitrary value */
    tmp = realloc(tmp, allocated);
    BUG_ON(!tmp);
}

tmp = realloc(tmp, pos);  /* free unneeded space */

When you are under Linux/POSIX, you can mmap the file and access memory directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM