32 位元程式使用 fstat 讀取 >2GB 檔案時,struct stat 的 st_size 為 32 位 long,無法容納大檔大小,報錯 Value too large for defined data type (EOVERFLOW)。
#define _FILE_OFFSET_BITS 64
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#include <sys/stat.h>
// 之後 off_t、struct stat 皆為 64 位
struct stat64 st;
fstat64(fd, &st); // st.st_size 為 64 位
printf("Size: %lld\n", (long long)st.st_size);
gcc -m64 ...,現代 Linux 發行版預設 64 位,問題自動消失。// 測試程式
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("bigfile", O_RDONLY);
struct stat st;
fstat(fd, &st);
printf("Size: %lld bytes\n", (long long)st.st_size);
close(fd);
return 0;
}
編譯執行後應正確顯示 3GB+ 大小。