linux环境下操作特大文件
gcc -D_FILE_OFFSET_BITS=64In 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'").
linux环境下操作特大文件 -D_FILE_OFFSET_BITS=64 今天特地仔细研究了一下linux环境下文件操作的一些细节,得到以下几个结论,如有不当之处,欢迎批评指正:) extern __off_t lseek (int __fd, __off_t __offset, int __whence) __THROW
off_t lseek(int fd, off_t offset, int whence);
# ifndef __USE_FILE_OFFSET64 typedef __off_t off_t; # else typedef __off64_t off_t;
#if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS == 64 # define __USE_FILE_OFFSET64 1 #endif
//#define _FILE_OFFSET_BITS 64 #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> int main() { int fd = open("large", O_RDONLY); if (fd == -1) { fprintf(stderr, "Failed to open.."); exit(EXIT_FAILURE); } printf("%d \n", sizeof(off_t));// 8 off_t offset = lseek(fd, (off_t)3*1024*1024*1024, SEEK_SET);//访问位于3G位置处的字符 if (offset == -1) { fprintf(stderr, "Failed to seek.."); exit(EXIT_FAILURE); } char ch; read(fd, &ch, 1); printf("the character = %c\n", ch); close(fd); exit(EXIT_SUCCESS); }
|