本文介绍了长度>时,不会读取Lion上的Fread. 2G的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Macosx Lion fread无法读取长度> 2G(int大小,2'147'483'648字节)的文件.它与macosx雪豹一起使用了多年.

Since Macosx Lion fread does not read file with length > 2G (int size, 2'147'483'648 bytes).It worked for years with macosx snow leopard.

我编写了一个程序对其进行测试:

I wrote a program to test it :

#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输出文件

没有错误.这两个文件的长度相同.这两个指纹不相同.(指针分配正确,并写入了输出文件)它仅适用于fread,而不适用于fwrite.

There is no error.The length of the 2 files are the same.The two fingerprints are not the same.(The pointer is well allocated and write in the outputfile)Its only with fread, not fwrite.

我不明白这个问题.

我只看到了这个程序(我不知道苹果是否在Lion上使用了这个程序),r变量定义为int. http://www.opensource.apple.com /source/Libc/Libc-186/stdio.subproj/fread.c

I just see this program (i don't know if apple use this one on Lion) and r variable is defined as int.http://www.opensource.apple.com/source/Libc/Libc-186/stdio.subproj/fread.c

感谢答案

推荐答案

在带有XCode 4.2的MacOS X Lion(10.7.2)上对我来说工作正常.可执行文件是64位程序.您应该确保自己也是.

Works fine for me on MacOS X Lion (10.7.2) with XCode 4.2. The executable is a 64-bit program. You should ensure yours is too.

$ make 2gb
/usr/bin/gcc -g -std=c99 -Wall -Wextra 2gb.c -o 2gb
2gb.c:5: warning: unused parameter ‘argc’
$ file 2gb
2gb: Mach-O 64-bit executable x86_64
$ dd if=/dev/zero of=input bs=1m count=3072
./2g3072+0 records in
3072+0 records out
3221225472 bytes transferred in 42.940363 secs (75016261 bytes/sec)
$ ls -l input
./2gb -rw-r--r--  1 jleffler  staff  3221225472 Oct 29 00:48 input
$ ./2gb input output
Input file length : 3221225472
$ openssl sha input
SHA(input)= c93bf6713a90e34554311f0a9e43cfd1f153475a
$ openssl sha output
SHA(output)= c93bf6713a90e34554311f0a9e43cfd1f153475a
$ ls -l input output
-rw-r--r--  1 jleffler  staff  3221225472 Oct 29 00:48 input
-rw-r--r--  1 jleffler  staff  3221225472 Oct 29 00:49 output
$ rm input output
$ /usr/bin/gcc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$

而且,当被迫进行32位编译时:

And, when forced into 32-bit compilation:

$ rm 2gb
$ make CC='/usr/bin/gcc -m32' 2gb
/usr/bin/gcc -m32 -g -std=c99 -Wall -Wextra 2gb.c -o 2gb
2gb.c:5: warning: unused parameter ‘argc’
$ dd if=/dev/zero of=input bs=1m count=3072
3072+0 records in
3072+0 records out
3221225472 bytes transferred in 38.326753 secs (84046397 bytes/sec)
$ ./2gb input output
Input file length : 0
$ ls -l input
-rw-r--r--  1 jleffler  staff  3221225472 Oct 29 00:57 input
$ 

这篇关于长度&gt;时,不会读取Lion上的Fread. 2G的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 14:22