简体   繁体   English

如何比较C中文件中的两个不同位置?

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

I am implementing the program to zip a file using a run-length encoding compression method. 我正在实现使用游程长度编码压缩方法压缩文件的程序。 Are there any ways you can compare the characters in the file or compare two file pointers to do that? 有什么方法可以比较文件中的字符或比较两个文件指针来做到这一点?

I opened the file(zipfilename) that I want to zip and set the file pointer named ftozip to it. 我打开了要压缩的文件(zipfilename),并设置了名为ftozip的文件指针。 Then, I tried to count the number of each character using this file pointer as shown in the code below (if condition). 然后,我尝试使用此文件指针计算每个字符的数量,如下面的代码所示(如果有条件)。

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;
            }
    }

That resulting in this error "invalid operands to binary == (have 'FILE' and 'FILE')". 导致此错误的“无效的二进制操作数==(具有'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