当长度 > 2G 时,Fread on Lion 无法读取

2024-02-23

由于 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

感谢您的解答


听起来你没有在 64 位模式下编译。查找您正在使用的任何编译器的命令行参数或选项。为了确保您在正确的模式下进行编译,printf("%d\n", sizeof(int));看看它是否显示了您所期望的内容。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当长度 > 2G 时,Fread on Lion 无法读取 的相关文章

随机推荐