最近需要用C操作文件,但是使用fopen和fseek的时候,在32位操作系统中,没办法操作2G以上的文件,后面经过多次Google和高手指点之后通过open64、lseek解决这个问题:
 1  #include <stdio.h>
 2 // #define _LARGEFILE_SOURCE
 3 // #define _LARGEFILE64_SOURCE
 4 // #define _FILE_OFFSET_BITS 64
 5 #include <sys/types.h>
 6 #include <sys/stat.h>
 7 #include <unistd.h>
 8 #include <stdio.h>
 9 #include <fcntl.h>
10 #include <errno.h>
11 
12 int main(int argc, char *argv[])
13 {
14         off_t  file_last_pos;
15         off_t end = 0;
16         //  FILE           *fp;
17         int fp = open64(argv[1], O_RDONLY);
18         if (fp < 0 ) {
19                 printf("can't open file [%s]\n", strerror(errno));
20                 return 1;
21         } else {
22                 printf("file open success\n");
23         }
24         file_last_pos = lseek(fp, 0, SEEK_END);
25         printf("Size: %1d \n",file_last_pos);
26         close(fp);
27         return 0;
28 }
//这行GCC参数很重要,原来是希望通过define的方式来解决的,但是最后还是只能通过这种方式
gcc -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64    test.c   -o test