2012年7月27日 星期五

fwrite 2GB file size limitation in Linux

Today our system popped a error message , about file size exceed limitation, and something wrong with login process. I just think about some software could store their data/logs over 2GB. Then "google" tell me why !!

[Question]

#include

int main(void) {
    char c[] = "abcdefghij";
    size_t rez;
    FILE *f = fopen("filldisk.dat", "wb");
    while (1) {
        rez = fwrite(c, 1, sizeof(c), f);
        if (!rez) break;
    }
    fclose(f);
    return 0;
}

When I run the program (in Linux), it stops when the file reaches 2GB

[Answers]

This is due to the libc (the standard C library), which by default on a x86 (IA-32) Linux system is 32-bit functions provided by glibc (GNU's C Library). So by default the file stream size is based upon 32-bits -- 2^(32-1).


[Solutions]
#define _FILE_OFFSET_BITS 64

#include
int main(void) {
    char c[] = "abcdefghij";
    size_t rez;
    FILE *f = fopen("filldisk.dat", "wb");
    while (1) {
        rez = fwrite(c, 1, sizeof(c), f);
        if ( rez < sizeof(c) ) {
            break;
        }
    }
    fclose(f);
    return 0;
}

Note: Most systems expect fopen (and off_t) to be based on 2^31 file size limit. Replacing them with off64_t and fopen64 makes this explicit, and depending on usage might be best way to go.


reference: http://stackoverflow.com/questions/730709/2gb-limit-on-file-size-when-using-fwrite-in-c
      http://www.suse.de/~aj/linux_lfs.html

沒有留言:

張貼留言

文章分類