簡體   English   中英

如何比較C中文件中的兩個不同位置?

[英]How to compare the two different positions in the file in C?

我正在實現使用游程長度編碼壓縮方法壓縮文件的程序。 有什么方法可以比較文件中的字符或比較兩個文件指針來做到這一點?

我打開了要壓縮的文件(zipfilename),並設置了名為ftozip的文件指針。 然后,我嘗試使用此文件指針計算每個字符的數量,如下面的代碼所示(如果有條件)。

FILE *ftozip;
ftozip = fopen(argv[1],"r");//open the file that we are zipping
if (ftozip == NULL) {//if there is an error opening
    perror("File cannot be opened ");
}
char zipfilename[30];
strcat(zipfilename, argv[1]);
strcat(zipfilename,".zip");
FILE *zipfilep = fopen(zipfilename, "a"); //zipfile openned to write

int count = 1;
while(1){ //incrementing the characters and storing in the zip  file
    if(*ftozip == *(ftozip +1)) {
        count++;
        char countchar[] = (char)count+(*ftozip);
        fputs(countchar, zipfilep);
        ftozip++;
        continue;
    }
    else {
        count = 1;
        countchar = (char)count + (*ftozip);
        ftozip++;
        if (feop(ftozip){
                break;
            }
            continue;
            }
    }

導致此錯誤的“無效的二進制操作數==(具有'FILE'和'FILE')”。

if(*ftozip == *(ftozip +1) ...if(*ftozip == *(ftozip +1) ...引用FILE*類型的指針不會訪問文件的內容。要按字節讀取或寫入文件,請使用freadfwrite代替。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM