由于Macosx Lion fread不读取长度为>的文件。 2G(int size,2'147'483'648字节)。 它与macosx雪豹合作多年。
我写了一个程序来测试它:
#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;
}
跑步:
没有错误。 2个文件的长度是相同的。 两个指纹不一样。 (指针很好分配并在输出文件中写入) 它只有fread,而不是fwrite。
我不明白这个问题。
我只是看到这个程序(我不知道苹果是否在Lion上使用这个程序)和 r变量定义为int。 http://www.opensource.apple.com/source/Libc/Libc-186/stdio.subproj/fread.c
感谢您的回答
答案 0 :(得分:1)
听起来好像你没有在64位模式下编译。查找命令行参数或您正在使用的任何编译器的选项。要确保您以正确的模式进行编译,printf("%d\n", sizeof(int));
并查看它是否显示了您的预期。
答案 1 :(得分:1)
使用XCode 4.2在MacOS X Lion(10.7.2)上运行正常。可执行文件是64位程序。你应该确保你的也是。
$ 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位编译时:
$ 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
$
答案 2 :(得分:0)
printf(“%d \ n”,sizeof(int)); gcc -Wall Typ.c -o Typ 警告:格式'%d'需要类型'int',但参数2的类型为'long unsigned int' 警告:格式'%d'需要类型'int',但参数2的类型为'long' - &GT; 4
谢谢乔纳森。
我在10.7.2和xcode 4.2
gcc --version i686-apple-darwin11-llvm-gcc-4.2(GCC)4.2.1(基于Apple Inc. build 5658)(LLVM build 2336.1.00)
我做gcc TestFile.c -o TestFile.o 甚至完全如你所做的那样: / usr / bin / gcc -g -std = c99 -Wall -Wextra 2gb.c -o 2gb 然后我在
后添加-m64我又有2个不同的指纹。
使用sudo / Library / uninstall-devtools --mode = all删除了xcode 3.6.2 和xcode 4.2安装之后。
我在新创建的另一个用户上运行pgm(可能是$ path的原因)并且它是相同的。
我不明白。
Apple团队在报告此错误后回答
HelloStéphane,
这是对Bug ID#10376104的跟进。经过进一步调查后,已确定这是一个已知问题,目前正在通过工程进行调查。此问题已在我们的错误数据库中以原始错误ID#6434977提交。用于跟踪此重复问题的原始错误号可在错误报告的“问题详细信息”视图的“相关问题”部分中找到。
感谢您提交此错误报告。我们非常感谢您协助我们发现和隔离错误。
最诚挚的问候,
开发人员错误报告小组 Apple全球开发者关系