在MinGW的确定64位的文件大小在C 32位 [英] Determine 64-bit file size in C on MinGW 32-bit
问题描述
我要疯了试图让这个在MinGW的32位工作。它适用于我已经尝试了所有其他平台。
I'm going crazy trying to get this to work in MinGW 32-bit. It works on all other platforms I've tried.
所有我想要做的就是一个> 4GB文件的大小为64位的int。
All I want to do is get the size of a > 4GB file into a 64-bit int.
这正常工作在其他平台上:
This works fine on other platforms:
#define _FILE_OFFSET_BITS 64
#include <sys/stat.h>
int64_t fsize(const char *filename) {
struct stat st;
if (stat(filename, &st) == 0)
return st.st_size;
return -1;
}
我试过上述code之前添加以下定义的基础上,我在网上找到各种建议:
I tried adding the following defines before the above code, based on various suggestions I found online:
#define _LARGEFILE_SOURCE 1
#define _LARGEFILE64_SOURCE 1
#define __USE_LARGEFILE64 1
也试过:
#ifdef __MINGW32__
#define off_t off64_t
#endif
最后尝试添加-D_FILE_OFFSET_BITS = 64到GCC标志(应该是一样的上述定义虽然...)
And finally tried adding -D_FILE_OFFSET_BITS=64 to the gcc flags (should be the same as the above define though...)
没有运气。返回的int64_t仍得到截断为32位值。
No luck. The returned int64_t is still getting truncated to a 32-bit value.
什么是决定MinGW的64位文件大小的正确方法32位?
What is the right way to determine a 64-bit file size in MinGW 32-bit?
谢谢!
推荐答案
谢谢你们,好建议,但我想它了... MinGW的需要此定义,以使结构__stat64和功能_stat64:
Thanks guys, good suggestions but I figured it out... MinGW requires this define to enable the struct __stat64 and the function _stat64:
#if __MINGW32__
#define __MSVCRT_VERSION__ 0x0601
#endif
那么这个作品:
int64_t fsize(const char *filename) {
#if __MINGW32__
struct __stat64 st;
if (_stat64(filename, &st) == 0)
#else
struct stat st;
if (stat(filename, &st) == 0)
#endif
return st.st_size;
return -1;
}
希望这可以帮助别人。
Hope this helps somebody.
这篇关于在MinGW的确定64位的文件大小在C 32位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!