我的代码存在一些问题,代码没有编译,但是当我测试代码时,我发现如果签名位于文件末尾(或文件中间),该程序不起作用。文件签名C
int normalScan(char * fileName, char * sign){
int toReturn = 0;
FILE * toScan = fopen(fileName,"rb");
FILE * signature = fopen(sign, "rb");
char * buffer = NULL;
char * secondBuffer = NULL;
int size = 0;
int secondSize = 0;
int elements = 0;
int i = 0;
fseek(signature, 0, SEEK_END);
size = ftell(signature);
rewind(signature);
fseek(toScan, 0, SEEK_END);
secondSize = ftell(toScan);
rewind(toScan);
buffer = malloc(sizeof(char)*size);
elements = fread(buffer, 1, size, signature);
secondBuffer = malloc(sizeof(char)*size);
elements = fread(secondBuffer, 1, size, toScan);
if (strcmp(secondBuffer, buffer) == 0){
toReturn = 1;
}
else{
while (fgetc(toScan) != EOF && fgetc(signature) != EOF){
rewind(signature);
fseek(toScan, -1, SEEK_CUR);
if (fgetc(toScan) == fgetc(signature)){
rewind(signature);
fseek(toScan, -1, SEEK_CUR);
elements = fread(secondBuffer, 1, size, toScan);
if (strcmp(secondBuffer, buffer) == 0){
toReturn = 1;
}
}
rewind(signature);
strncpy(secondBuffer, "", sizeof(secondBuffer));
}
}
free(secondBuffer);
fclose(toScan);
fclose(signature);
free(buffer);
return toReturn;
}
请看[为什么“while(!feof(file))”总是出错?](http://stackoverflow.com/q/5431941/2173917) –
[不要将'malloc'的结果强制转换为C](http://stackoverflow.com/q/605845/995714) –
你可能想说几句关于你的代码打算做什么的话。代码本身并不是很清楚。它是否在文件中搜索已知的签名? –