2016-05-28 4 views
-3

2つのバイナリファイルを読み込み、1つのファイルに少なくとも2番目のファイルの内容があるかどうかを確認しようとしました。バイナリファイルに少なくとも2番目のファイルの内容が含まれていることを確認してください

#include <stdio.h> 

int main(int argc, char *argv[]) 
{ 
    FILE *file1, *file2; 
    file1 = fopen(argv[1], "rb"); 
    file2 = fopen(argv[2], "rb"); 

    if(file1 == NULL) 
    { 
     printf("Error: can't open file number one.\n"); 
    } 
    if(file2 == NULL) 
    { 
     printf("Error: can't open file number two.\n"); 
    } 
    else 
    { 
    /* if the files can open... start to check... */ 
    } 
} 
+1

そして、あなたの具体的な質問は何ですか?あなたが示したコードはあまり効果がありません。実際のチェックロジックは試行されません。あなたのために仕事をするように私たちに求めていますか?純粋な*試行をしたり、少なくとも特定の質問をしたりすると、回答が得られる可能性が高くなります。 – kaylum

+0

2つのファイルをバイナリで比較したいのですが、最初のファイルの内容は2になりますが完全ではありません。最初のファイル内容:abcと2番目のファイル内容:fdsdfsdfsabc ...これはバイナリ形式です – Skittles

+2

これはあなたがしたいことです。しかしそれは疑問ではありません。あなたは、あなたがする方法を知らない特定のことについて私達に尋ねなければなりません。私たちはあなた自身でコードを書くことができますが、コードを書くだけではありません。あなたはファイルからの読み方を知っていますか?データバッファを比較する方法は?それは何ですか*具体的に*あなたはあなたが助けを必要とする方法を知らない? – kaylum

答えて

1

をあなたが文字で2つのファイルに文字を比較する(それがバイナリかテキストだ場合、それは問題ではありません)と2つのファイル〜ステップ機能を使用して文字を比較することができます:私が試した何

文字ごとに

#include <stdio.h> 

void compare(FILE *, FILE *); 

int main(int argc, char *argv[]) { 
    FILE *file1, *file2; 

    file1 = fopen(argv[1], "r"); 
    if (file1 == NULL) { 
     printf("Error: can't open file number one.\n"); 
     return 0; 
    } 

    file2 = fopen(argv[2], "r"); 
    if (file2 == NULL) { 
     printf("Error: can't open file number two.\n"); 
     return 0; 
    } 

    if ((file1 != NULL) && (file2 != NULL)) { 
     compare(file1, file2); 
    } 
} 

/* 
* compare two binary files 
*/ 
void compare(FILE *file1, FILE *file2) { 
    char ch1, ch2; 
    int flag = 0; 

    while (((ch1 = fgetc(file1)) != EOF) && ((ch2 = fgetc(file2)) != EOF)) { 
     /* 
      * if equal then continue by comparing till the end of files 
      */ 
     if (ch1 == ch2) { 
      flag = 1; 
      continue; 
     } 
      /* 
       * If not equal then returns the byte position 
       */ 
     else { 
      fseek(file1, -1, SEEK_CUR); 
      flag = 0; 
      break; 
     } 
    } 

    if (flag == 0) { 
     printf("Two files are not equal : byte position at which two files differ is %d\n", ftell(file1) + 1); 
    } 
    else { 
     printf("Two files are Equal\n"); 
    } 
} 

テスト

$ ./a.out a.out a2.out 
Two files are not equal : byte position at which two files differ is 209 
$ cp a.out a2.out 
$ ./a.out a.out a2.out 
Two files are Equal 
関連する問題