简体   繁体   中英

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. 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')".

if(*ftozip == *(ftozip +1) ...if(*ftozip == *(ftozip +1) ...引用FILE*类型的指针不会访问文件的内容。要按字节读取或写入文件,请使用freadfwrite代替。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM