2014-07-19 64 views
1

我目前正在嘗試編寫一次讀取兩個文件一個字節的程序(是的,我意識到繁重的I/O開銷),但是我在增加FILE指針。我想編程來比較兩個文件逐字節,並且getc不會是一個可行的選項,因爲它只能用於字符,因爲字符是一個字節。但是,我正在閱讀兩個文本文件,文本文件可能包含諸如整數,雙精度等數字。因此,在這種情況下,我想從int/double的一部分中獲取該字節並將其與其他文件進行比較(逐個字節的比較)。一次讀取一個字節的兩個文件

這是我到目前爲止有:

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 
#include <time.h> 

#define BUFFER_SIZE 1 

unsigned char buffer1[BUFFER_SIZE]; 
unsigned char buffer2[BUFFER_SIZE]; 

int main() 
{ 
    FILE *fp1, *fp2; 
    int ch1, ch2; 
    clock_t elapsed; 
    char fname1[40], fname2[40]; 

    printf("Enter name of first file :"); 
    fgets(fname1, 40, stdin); 
    while (fname1[strlen(fname1) - 1] == '\n') 
    { 
     fname1[strlen(fname1) -1] = '\0'; 
    } 

    printf("Enter name of second file:"); 
    fgets(fname2, 40, stdin); 
    while (fname2[strlen(fname2) - 1] == '\n') 
    { 
     fname2[strlen(fname2) -1] = '\0'; 
    } 

    fp1 = fopen(fname1, "r"); 
    if (fp1 == NULL) 
    { 
     printf("Cannot open %s for reading\n", fname1); 
     exit(1); 
    } 

    fp2 = fopen(fname2, "r"); 
    if (fp2 == NULL) 
    { 
     printf("Cannot open %s for reading\n", fname2); 
     exit(1); 
    } 

    elapsed = clock(); // get starting time 

    /* Read in 256 8-bit numbers into the buffer */ 
    size_t bytes_read1 = 0; 
    size_t bytes_read2 = 0; 

    bytes_read1 = fread(buffer1, sizeof(unsigned char), BUFFER_SIZE, fp1); 
    bytes_read2 = fread(buffer2, sizeof(unsigned char), BUFFER_SIZE, fp2); 

    printf("%c + in buffer 1\n", *buffer1); 
    printf("%c + in buffer 2\n", *buffer2); 

    fclose (fp1); // close files 
    fclose (fp2); 

    elapsed = clock() - elapsed; // elapsed time 
    printf("That took %.4f seconds.\n", (float)elapsed/CLOCKS_PER_SEC); 
    return 0; 
} 

我假設緩衝器1和緩衝器是一個字節的內容被讀取?我需要將它們轉換爲數字來比較它們嗎?我想我可以做的比較如下

(buffer1^buffer2) == 0 

,那麼這將意味着他們是平等的基礎上,XOR位運算

感謝您的幫助提前

+1

「GETC不會是一個可行的選擇」 - 你是非常,很困惑。你怎麼猜「數字,如整數,雙打等」存儲在一個文件中? getc獲得一個字節,您的fread也是如此,但getc的效率更高。 '(buffer1^buffer2)== 0'與'buffer1 == buffer2'的作用相同,但兩者都比較地址,而不是字節。 –

+0

@JimBalter多於一個字節 – humblebeast

+0

「多於一個字節」只是一個字節序列。 –

回答

1

我很高興在戲謔評論。也許是時候舉個例子吧。

:在文本文件中,字母字符,如 「a」 將被解釋爲 'A'(97,或0x61)。數字字符(如「2」)的解釋方式與「2」(50或0x32)相同。文件只是使用fgetc()的字母數字,標點符號或空白字符的集合,可以一次查看一個字符。

與你的斷言相反,fgetc()不適用於逐字節比較,下面是一個簡單的例子,顯示它的確如此。表示使用fgetc()與輸入和結果代碼相同內容文件,和不同內容文件:

#include <ansi_c.h>//this is a collector of the ansi C headers. Pick the one in your 
        //environment that work for you. 
#include <limits.h> 

#define FILE1 "C:\\dev\\play\\file1.txt" 
#define FILE2 "C:\\dev\\play\\file2.txt" 

BOOL CompareFileByteByByte(char *file1, char *file2); 

int main(void) 
{ 

    if(CompareFileByteByByte(FILE1, FILE2)) 
    { 
     printf("Files are equal\n"); 
    } 
    else 
    { 
     printf("Files are NOT equal\n"); 
    } 


    return 0; 
} 

BOOL CompareFileByteByByte(char *file1, char *file2) 
{ 
    FILE *fp1=0, *fp2=0; 
    BOOL results = 0; 

    int c1 = 0, c2 = 0;//note, even though getc reads one char from file, 
         //it uses int as return to accomodate -1 (EOF) 

    fp1 = fopen(FILE1, "r"); 
    fp2 = fopen(FILE2, "r"); 


    c1 = fgetc(fp1); 
    c2 = fgetc(fp2); 

    results = (c1 == c2); 

    while((c1!=EOF) && (c2 != EOF) && results) 
    { 
     c1 = fgetc(fp1); 
     c2 = fgetc(fp2); 
     results = (c1 == c2); 
    } 

    return results; 
} 

鑑於FILE1 FILE2:(兩者相同)

Oringinal text... 
...more text 123456 
...more text 2.3456 
...more text 3e12 

結果文件相同

鑑於FILE1

Oringinal text... 
...more text 123456 
...more text 2.3456 
...more text 3e12 

和file2

Oringinal text... 
...more text 123456 
...more text 2.3456 
...more text 4e12 

結果文件是不相等的

+0

非常感謝,我今天聽到的最有用的信息 – humblebeast

+0

@humblebeast - 在發佈這個答案後,我查看了最近的帖子歷史。看來您在比較文件時對此感興趣。你見過這些其他的方法/討論:*** [1](http://stackoverflow.com/a/20688284/645128)***,*** [2](http://www.dreamincode.net/forums/topic/236817-how-will-i-compare-two-files /)***,*** [3](http://objectmix.com/asm-x86-asm-370/166774-byte字節 - 比較 - 重複的文件取景器,killer.html)***。 – ryyker

相關問題