解决Linux中在创建大于4g文件时遇到的File Size Limit Exceeded问题

今天在解决在PMC开发板上挂着NTFS磁盘分区,创建大于4G文件时老在文件到达2G时出现以下错误:

File Size Limit Exceeded

于是在我的Fedora 9虚拟机上上用gcc编译程序进行测试,还是遇到同样的问题。通过命令”ulimit -f“发现是没有限制的,当山感觉到可能是我的程序出了问题。于是在网上google和百度了一番。

终于发现了问题所在:

我们通常的linux系统,offset_t的大小为32,通过程序创建的话,系统不支持大于2G的文件。

 

解决方法:

 

编译时加上-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 即可。目的是用于调整offset_t的大小,由于的32位INT变成64位。

 

如下所示:

[root@localhost programs]# gcc -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64  -o write_bigfile write_bigfile.c

 

在这过程中了解学习了ulimit命令的用途:于shell启动进程所占用的资源。

同样,如果要跑到嵌入式平台,只需用进行交叉编译即可。  

 

附上write_bigfile.c的代码:

 

/*这份代码经源码格式软件格式化过 [email protected] */ #include #include #include #include #include #include #include #include #include #include #include #define _FILE_OFFSET_BITS 64 #define _LARGEFILE64_SOURCE #define __USE_LARGEFILE64 #define LLONG64 long long #define COUNT 1024 #define NUM 5 #define MYSIZE 1024*1024 int safe_write(FILE *pfile, void *buf, size_t size) { int w_size=0 ; int ret=0 ; //安全测试省略 while(w_size0) { w_size+=ret ; } else { break ; } } return w_size ; } int main() { int i,j ; char mybuf[MYSIZE]= "1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n1234567890abcdefghijklmnopqrstu/n0987654321zyxwvutsrqponmlkjihgf/n"; FILE *Sfp ; LLONG64 llSeekBeg ; if((Sfp=fopen("/root/programs/lfs_test-gnu","awb"))==NULL) { printf("File cannot be opended /n"); return ; } for(i=0;i

你可能感兴趣的:(linux)