如何在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;
}
}
导致此错误 "invalid operands to binary == (have ‘FILE’ and ‘FILE’)"。
取消引用 FILE*
类型的指针,就像您对 if(*ftozip == *(ftozip +1) ...
所做的那样,不会访问文件的内容。
要按字节读取或写入文件,请改用 fread
和 fwrite
。
我正在执行程序以使用 运行 长度编码压缩方法压缩文件。有什么方法可以比较文件中的字符或比较两个文件指针吗?
我打开了要压缩的文件 (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;
}
}
导致此错误 "invalid operands to binary == (have ‘FILE’ and ‘FILE’)"。
取消引用 FILE*
类型的指针,就像您对 if(*ftozip == *(ftozip +1) ...
所做的那样,不会访问文件的内容。
要按字节读取或写入文件,请改用 fread
和 fwrite
。