由于 Macosx Lion fread 不读取长度 > 2G(int 大小,2'147'483'648 字节)的文件。
它在 macOS Snow Leopard 上工作了很多年。
我写了一个程序来测试它:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *fin = NULL, *fout = NULL;
char *ptr = NULL;
size_t len;
fpos_t flen;
if (!(fin = fopen(argv[1], "rb")))
{
printf("The input file: %s could not be opened\n", argv[1]);
return -1;
}
if ((fout = fopen(argv[2], "rb")))
{
printf("The output file %s already exist\n", argv[2]);
fclose(fin);
return -1;
}
if (!(fout = fopen(argv[2],"wb")))
{
printf("Cannot write on output file %s\n", argv[2]);
fclose(fin);
return -1;
}
fseek(fin, 0, SEEK_END);
fgetpos(fin, &flen);
len = flen;
printf("Input file length : %zd\n", len);
fseek(fin, 0, SEEK_SET);
if (!(ptr = malloc(len)))
{
printf("Canot allocate %zd bytes\n", len);
fclose(fin);
fclose(fout);
return -1;
}
if (fread(ptr, sizeof(char), len, fin) != len)
{
printf("Cannot read file\n");
fclose(fin);
fclose(fout);
free(ptr);
return -1;
}
fclose(fin);
if (fwrite(ptr, sizeof(char), len, fout) != len)
{
printf("Cannot write file\n");
fclose(fout);
free(ptr);
return -1;
}
free(ptr);
fclose(fout);
return 1;
}
赶紧跑 :
- ./pgm 输入文件 输出文件
- openssl sha 输入文件
- openssl sha 输出文件
没有错误。
2个文件的长度相同。
两个指纹不一样。
(指针已分配好并写入输出文件中)
它只与 fread 相关,而不与 fwrite 相关。
我不明白这个问题。
我刚刚看到这个程序(我不知道苹果是否在 Lion 上使用这个程序)并且
r 变量被定义为 int。http://www.opensource.apple.com/source/Libc/Libc-186/stdio.subproj/fread.c http://www.opensource.apple.com/source/Libc/Libc-186/stdio.subproj/fread.c
感谢您的解答