访问超过4G的文件,在Linux或Unix下该怎么做?

aw325 2011-01-02 12:28:06
加精
现在想读取一个大文件,可能是几个G或者几十G的文件,可否直接使用fopen打开,fread读取等平时我们用的方法?
在windows下是不可以的,不知道Unix、Linux下是否有限制,或者该怎么解决!

请给一小段代码实例,谢谢!
...全文
10289 94 打赏 收藏 转发到动态 举报
写回复
用AI写文章
94 条回复
切换为时间正序
请发表友善的回复…
发表回复
czc1009 2013-08-28
  • 打赏
  • 举报
回复
此问题没遇到过,一般来说块打了就会分的。回去试下,,,
firnsan 2013-08-28
  • 打赏
  • 举报
回复
在linux下fopen对要打开的文件大小是有限制的。 对于32位程序,fopen无法打开大于2G的文件,但可用下面的方法突破限制: 1:编译成64位程序 2:使用fopen64,fseek64 3:文件开头加上#define _FILE_OFFSET_BITS 64
chengzhe 2011-11-29
  • 打赏
  • 举报
回复
mmap 感觉是这个
zhengyubellson 2011-10-21
  • 打赏
  • 举报
回复
7楼说的是linux下的环境编译参数,9楼说的是AIX系统的64位编译参数,在AIX下加-D_LARGE_FILES -D_LARGE_FILES_API这两个参数即可把stat()编译成为stat64()
不过换成了这个大文件系统,很多应用程序会面临兼容的问题,其中在编译的时候所有的STL会自动加上std::_LFS_ON参数,这样的话要重新编译所有的STL相关程序,包括静态库和动态库,如果应用中有直接用第三方的的动态库的,这种情况就会有问题,编译ld连接会报错,例如用到occi的情况。。。
建议最好是别用这种大文件环境,我折腾了好几天最后还是放弃了。。。
把文件拆小就行了
liuchai910 2011-02-21
  • 打赏
  • 举报
回复
你应该明确自己的文件是什么格式的文件,因为文件格式不同会运用不用的文件打开函数进行打开。
在linux系统中,fopen,open,gzopen等函数均可以打开一个文件,然后对其进行相应的操作。
tooh 2011-02-20
  • 打赏
  • 举报
回复
可以对应的函数 如read64 write64 可以访问到2^63字节 也可以编译时加选项 上面有人给了
hawk198 2011-02-20
  • 打赏
  • 举报
回复
看的眼睛都花了,也不知道哪个答案可行啊,首先是创建一个4G的文件,然后进行读写,希望能有个实例程序看看
踏岸寻柳 2011-01-15
  • 打赏
  • 举报
回复

#define _FILE_OFFSET_BITS 64

使用该宏后写大文件问题不大,只是不清楚如何偏移、如何读。因为,无论是低级I/O,还是高级I/O,它们能够访问的偏移量都是32位的。
nini7809 2011-01-13
  • 打赏
  • 举报
回复
一个简单的问题竟然这么多人围观?
#man lseek64
不就啥都清楚了?

-----------------------------------------
NAME
lseek64 - reposition 64-bit read/write file offset

SYNOPSIS
#define _LARGEFILE64_SOURCE
#include <sys/types.h>
#include <unistd.h>

off64_t lseek64(int fd, off64_t offset, int whence);

DESCRIPTION
The lseek(2) family of functions reposition the offset of the open file associated with the file descrip-
tor fd to offset bytes relative to the start, current position, or end of the file, when whence has the
value SEEK_SET, SEEK_CUR, or SEEK_END, respectively.

For more details, return value, and errors, see lseek(2).

Four interfaces are available: lseek(), lseek64(), llseek(), and the raw system call _llseek().

lseek
Prototype:

off_t lseek(int fd, off_t offset, int whence);

The library routine lseek() uses the type off_t. This is a 32-bit signed type on 32-bit architectures,
unless one compiles with

#define _FILE_OFFSET_BITS 64

in which case it is a 64-bit signed type.

lseek64
Prototype:

off64_t lseek64(int fd, off64_t offset, int whence);

The library routine lseek64() uses a 64-bit type even when off_t is a 32-bit type. Its prototype (and the
type off64_t) is available only when one compiles with

#define _LARGEFILE64_SOURCE

The function lseek64() is available since glibc 2.1, and is defined to be an alias for llseek().

。。。 。。。
mymtom 2011-01-13
  • 打赏
  • 举报
回复
标准方法, 适用于POSIX兼容系统。
http://www.opengroup.org/onlinepubs/000095399/utilities/c99.html
getconf POSIX_V6_ILP32_OFFBIG_CFLAGS;
getconf POSIX_V6_ILP32_OFFBIG_LDFLAGS;
getconf POSIX_V6_ILP32_OFFBIG_LIBS;
lzj1980 2011-01-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 plusplus2010 的回复:]
这个在linux下面叫large-file support (LFS),在源码开头加上

C/C++ code
#define _FILE_OFFSET_BITS 64

就可以了。
[/Quote]正解
yuankai19881108 2011-01-12
  • 打赏
  • 举报
回复
是不是7楼的正确
cnlm2 2011-01-10
  • 打赏
  • 举报
回复
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc, char *argv[])
{
int fd;

fd = open("a.txt", O_RDWR|O_CREAT|O_LARGEFILE, 0644);
if (fd==-1){
error("open");
exit(-1);
}

close(fd);

return 0;
}

如上代码定义上面那3个宏,然后再open的参数里加一个O_LARGEFILE,即可!
yskcg 2011-01-10
  • 打赏
  • 举报
回复
一个进程不是只有4G吗?如果超过,我还真没有想过。
chenee543216 2011-01-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 phpjspasp 的回复:]

mmap
[/Quote]
nod
「已注销」 2011-01-09
  • 打赏
  • 举报
回复
LINUX现在都有支持64位的了
wjfwgd 2011-01-09
  • 打赏
  • 举报
回复
是不是7楼的正确
A05125113 2011-01-09
  • 打赏
  • 举报
回复
各位高手,受益匪浅啊
xiaobaitu8629 2011-01-09
  • 打赏
  • 举报
回复
这么大的文件啊,,学习啊~~
W9757 2011-01-09
  • 打赏
  • 举报
回复
这个真的不会.
加载更多回复(54)

23,124

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧