尝试使用文件i / o读写来查找两个文件不同的字节位置

时间:2014-01-29 22:46:21

标签: c file-io

我正在尝试比较两个文件以使用读取和写入命令找到它们的不同之处我在某种意义上从一本书的例子开始,并认为我得到了比较函数,但我显然没有因为sprintf功能打印出来。我最好的猜测是我如何比较这两个文件?:

(更新后的代码)

 Byte pos where two files differ is:
 Byte value of file 1:  %o
 Byte value of file 2: %o

 void compare_two_binary_files(int f1, int f2)
 {
      write(1, sprintf, 10);
      ssize_t byte_read_f1, byte_read_f2, length, numRead, bob;
      char buf1[BUF_SIZE], buf2[BUF_SIZE], a[100], b[100], counter[100];
      int count = 0, b_pos1, b_pos2;
      while ((byte_read_f1 = read(f1, buf1, sizeof buf1) > 0) && (byte_read_f2 = read(f2, buf2, sizeof buf2) >0)) {
            ssize_t len =  byte_read_f1 <byte_read_f2 ? byte_read_f1 : byte_read_f2;
            if (memcmp(buf1, buf2, len) != 0){
                 ssize_t i;
                 sprintf(counter, "Byte pos where two files differ is:%lld\n", (long long) count + i);
                 sprintf(a, "Byte value of file 1: %hho\n", buf1[i]);
                 sprintf(b, "Byte value of file 2: %hho\n", buf2[i]);
                 break;
            }
            count += len;
 }

新代码,但仍然错误,没有通过if“!=”语句,因为它只是在我想要的时候没有通过它。请帮助:

 void compare_two_binary_files(int f1, int f2)
 {
    ssize_t byte_read_f1, byte_read_f2, length, i;
    char buf1[BUF_SIZE], buf2[BUF_SIZE], a[BUF_SIZE], b[100], counter[100];
    int count = 0;
    while ((byte_read_f1 = read(f1, buf1, BUF_SIZE) > 0) && (byte_read_f2 = read(f2, buf2, BUF_SIZE) >0)){
            if (byte_read_f1 != byte_read_f2){
                    sprintf(counter, "Byte pos where two files differ is:%lld\n", (long long)  count + 1);
                    sprintf(a, "Byte value of file 1:  %o\n", buf1[i]);
                    sprintf(b, "Byte value of file 2: %o\n", buf2[i]);
                    write(1, counter, 100);
                    write(1, a, 100);
                    write(1, b, 100);
            }
            i++;
            count++;
    }
 }

我现在在这里。而且它不会进入if (memcmp(buf1, buf2, len) != 0){

 void compare_two_binary_files(int f1, int f2)
 {
    //write(1, sprintf, 10);
    ssize_t byte_read_f1, byte_read_f2, length, numRead, bob;
    char buf1[BUF_SIZE], buf2[BUF_SIZE], a[100], b[100], counter[100];
    int count = 0, b_pos1, b_pos2, hey;
    while ((byte_read_f1 = read(f1, buf1, sizeof buf1) > 0) && (byte_read_f2 = read(f2, buf2, sizeof buf2) >0)) {
            ssize_t len =  byte_read_f1 <byte_read_f2 ? byte_read_f1 : byte_read_f2;
            if (memcmp(buf1, buf2, len) != 0){
                    ssize_t i;
                    for (i = 0; i<len; i++){
                            if (buf1[i] != buf2[i]) break;
                    }
                    sprintf(counter, "Byte pos where two files differ is:%lld\n", (long long) count + i);
                    write(1, counter, 100);
                    sprintf(a, "Byte value of file 1: %hho\n", buf1[i]);
                    sprintf(b, "Byte value of file 2: %d\n", buf2[i]);
                    write(1, counter, 100);
                    write(1, b, 100);
                    write(1, a, 100);
                    break;
            }
            count += len;
    }
 }

当我将memcmp的!=更改为==时,它可以正常工作,但却给了我错误的数字。我应该得到字节位置4,文件1为65,文件2为143,但得到pos 1,文件1是163,文件2是115

1 个答案:

答案 0 :(得分:1)

需要单独的缓冲区并使用memcmp和/或循环来比较字节 请注意,文件长度可能不同。

// Untested code
char buf1[BUF_SIZE];
char buf2[BUF_SIZE];
while (((byte_read_f1 = read(f1, buf1, sizeof buf1)) > 0) && 
       ((byte_read_f2 = read(f2, buf2, sizeof buf2)) > 0)) {
  // find min length
  ssize_t len =  byte_read_f1 <byte_read_f2 ? byte_read_f1 : byte_read_f2;
  if (memcmp(buf1, buf2, len) != 0) {  // use memcmp for speed
    ssize_t i;
    for (i = 0; i<len; i++) {
      if (buf1[i] != buf2[i]) break;
    } 
    sprintf(counter, "Byte pos where two files differ is:%lld\n", (long long) count + i);
    sprintf(a, "Byte value of file 1: %hho\n", buf1[i]);
    sprintf(b, "Byte value of file 2: %hho\n", buf2[i]);
    break;
  }
  count += len;
  // If lengths differ
  if (byte_read_f1 != byte_read_f2) {
    sprintf(counter, "Byte pos where two files differ in length: %lld\n", (long long) count);
    // TBD code for a and b
    break;
    }
}