我正在尝试编写一个 bittorrent 客户端。为了解析文件等,我需要将一个 torrent 文件读入内存。我注意到 fread 没有将整个文件读入我的缓冲区。经过进一步调查,似乎只要在文件中遇到下面显示的符号,fread 就会停止读取文件。在 FILE* 指针上调用 feof 函数返回 16,表示已到达文件末尾。无论符号放置在何处,都会发生这种情况。有人可以解释为什么会发生这种情况以及任何可能有效的解决方案。
该符号在下面突出显示:
这是执行读取操作的代码:
char *read_file(const char *file, long long *len){
struct stat st;
char *ret = NULL;
FILE *fp;
//store the size/length of the file
if(stat(file, &st)){
return ret;
}
*len = st.st_size;
//open a stream to the specified file
fp = fopen(file, "r");
if(!fp){
return ret;
}
//allocate space in the buffer for the file
ret = (char*)malloc(*len);
if(!ret){
return NULL;
}
//Break down the call to fread into smaller chunks
//to account for a known bug which causes fread to
//behave strangely with large files
//Read the file into the buffer
//fread(ret, 1, *len, fp);
if(*len > 10000){
char *retTemp = NULL;
retTemp = ret;
int remaining = *len;
int read = 0, error = 0;
while(remaining > 1000){
read = fread(retTemp, 1, 1000, fp);
if(read < 1000){
error = feof(fp);
if(error != 0){
printf("Error: %d\n", error);
}
}
retTemp += 1000;
remaining -= 1000;
}
fread(retTemp, 1, remaining, fp);
} else {
fread(ret, 1, *len, fp);
}
//cleanup by closing the file stream
fclose(fp);
return ret;
}
感谢您的时间 :)