Linux下大文件读写源码示例

#define _LARGEFILE_SOURCE  
#define _LARGEFILE64_SOURCE  
#define _FILE_OFFSET_BITS   64  

#include <sys/types.h>  
#include <sys/stat.h>  
#include <unistd.h>  
#include <stdio.h>  
#include <fcntl.h>  

int main()  
{  
    printf("sizeof(off_t) = %d\n", sizeof(off_t));  
    int fd = 0;  
#if 1  
    fd = open("/root/test", O_WRONLY | O_CREAT | O_LARGEFILE, 0600);  
#else  
    fd = open("/dev/hda1", O_RDONLY, 0600);  
#endif  
    if (fd > 0)  
    {  
        off_t l = 0x200000001LL;  
        off_t l_new = lseek(fd, l, SEEK_SET);  
        if (l_new < 0)  
            printf("l_new =%d\n", l_new);  
#if 1  
        else
            write(fd, &fd, sizeof(fd));  
#endif  
        printf("l_new = 0x%.16x\n", l_new);  
        close(fd);  
    }  
    struct stat st;  
    int ret = stat( "/root/test", &st);  
    printf( "\n%d--%I64u-\n", ret, st.st_size );   
}