linux 大檔案相關資料

來源:互聯網
上載者:User

http://www.suse.de/~aj/linux_lfs.html

全面介紹

Using LFS

For using LFS in user programs, the programs have to use the LFS API. This involves recompilation and changes of programs. The API is documented in the glibc manual (the libc info pages) which can be read with e.g. "info libc".

In a nutshell for using LFS you can choose either of the following:

  • Compile your programs with "gcc -D_FILE_OFFSET_BITS=64". This forces all file access calls to use the 64 bit variants. Several types change also, e.g. off_t becomes off64_t. It's therefore important to always use the correct types and to not use e.g. int instead of off_t. For portability with other platforms you should use getconf LFS_CFLAGS which will return -D_FILE_OFFSET_BITS=64 on Linux platforms but might return something else on e.g. Solaris. For linking, you should use the link flags that are reported via getconf LFS_LDFLAGS. On Linux systems, you do not need special link flags.
  • Define _LARGEFILE_SOURCE and _LARGEFILE64_SOURCE. With these defines you can use the LFS functions like open64 directly.
  • Use the O_LARGEFILE flag with open to operate on large files.

A complete documentation of the feature test macros like _FILE_OFFSET_BITS and _LARGEFILE_SOURCE is in the glibc manual (run e.g. "info libc 'Feature Test Macros'").

The LFS API is also documented in the LFS standard which is available at http://ftp.sas.com/standards/large.file/x_open.20Mar96.html.

《---------------------------------------------------------------------------------------------------------------------------------------------》

http://docs.python.org/release/2.1.3/lib/posix-large-files.html

of Irix, but with Solaris 2.6 and 2.7 you need to do something like:

CC="cc `getconf LFS_CFLAGS`" ./configure

On large-file-capable Linux systems, this might work:

CC='gcc -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64' ./configure

《---------------------------------------------------------------------------------------------------------------------------------------------》

http://blog.csdn.net/yuhao1984/article/details/6116799

linux大檔案

2011-01-04 23:51 37人閱讀 評論(0) 收藏 舉報

1. 如何查看linux系統的位元(32/64):

    * 直接看看有沒有/lib64目目錄的方法:

           64位的系統會有/lib64和/lib兩個目錄,32位只有/lib一個

    * getconf LONG_BIT:

          32位的系統中int類型和long類型一般都是4位元組,64位的系統中int類型還是4位元組的,但是long已變成了8位元組

    * uname -a:

如果是64位,會有x86_64的字樣

    * 用編程的方法,sizeof( long 或 size_t )就是系統的位元

這個與編譯器版本有關,有時會不相符。不建議使用。所以在編程中,用可以指定int的長度int32_t,int64_t,uint32_t和uint64_t等可以避免位元問題和增強可讀性

2. linux下C語言操作大檔案(4G以上)

一般情況下,64位作業系統可以操作任何大檔案(2^64)。

以下討論的是32位機器的情況。

首先32位機器用fopen/fclose開啟大檔案沒有問題,順序讀寫操作while(!feof(fp)){ fread / fgets / fscanf }或while(1){ fwrite / fputs / fprintf} 也沒有問題。

由於32位機器下long是32位,故

fseek (FILE *stream, long offset, int whence)

long ftell(FILE *stream) 不能訪問4G以上檔案

此時要用 fseeko (FILE *stream, off_t offset, int whence)

off_t ftello(FILE *stream);代替

類型off_t 的定義在 <sys/types.h>裡面:

# ifndef __USE_FILE_OFFSET64

typedef __off_t off_t;

# else

typedef __off64_t off_t;

# endif

off_t在32位機器中是32bit,64位機器中是64bit。那麼,在32位機器中,在include之前加入宏定義:#define _FILE_OFFSET_BITS 64,或者編譯是加入-D_FILE_OFFSET_BITS 64告訴系統在檔案內部使用64位的位移地址,使off_t變成__off64_t類型。

這樣,只要你用64bit的類型(off_t,long(64位機器),和long long(32位機器) 或int64_t/uint64_t)聲明offset作為fseeko的參數輸入,就可以操作4G以上的檔案了。

許多人還提到在宏定義中多寫上一些:

#define _FILE_OFFSET_BITS 64

#define _LARGEFILE_SOURCE

#define _LARGEFILE64_SOURCE

但另外兩個的具體功能我現在尚未搞得很清楚,等以後瞭解完全再補上。

*   使用LINUX自己的庫函數進行檔案操作(*nix I/O操作),加入O_LARGEFILE選項:

#define __USE_LARGEFILE64

#include <fcnl.h>

int fp = open("myfile", O_WRONLY | O_TRUNC | O_CREAT | O_LARGEFILE, 644);

《---------------------------------------------------------------------------------------------------------------------------------------------》

Adding Support for Arbitrary File Sizes to the Single UNIX Specification

http://www.unix.org/version2/whatsnew/lfs20mar.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.