Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MinGW 32位上确定C中的64位文件大小_C_Mingw_Filesize - Fatal编程技术网

在MinGW 32位上确定C中的64位文件大小

在MinGW 32位上确定C中的64位文件大小,c,mingw,filesize,C,Mingw,Filesize,我要发疯了,想让它在MinGW 32位中工作。它可以在我尝试过的所有其他平台上运行 我所要做的就是将大于4GB的文件大小转换为64位整数 这在其他平台上运行良好: #define _FILE_OFFSET_BITS 64 #include <sys/stat.h> int64_t fsize(const char *filename) { struct stat st; if (stat(filename, &st) == 0) re

我要发疯了,想让它在MinGW 32位中工作。它可以在我尝试过的所有其他平台上运行

我所要做的就是将大于4GB的文件大小转换为64位整数

这在其他平台上运行良好:

#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; 
}
还尝试:

#ifdef __MINGW32__
#define off_t off64_t
#endif
最后尝试将-D_FILE_OFFSET_BITS=64添加到gcc标志中(应该与上面的定义相同…)

不走运。返回的int64_t仍被截断为32位值

在MinGW 32位中,确定64位文件大小的正确方法是什么


谢谢

如果MinGW包中存在lseek64,您可以尝试
lseek64

   #define _LARGEFILE64_SOURCE     /* See feature_test_macros(7) */
   #include <sys/types.h>
   #include <unistd.h>

   off64_t lseek64(int fd, off64_t offset, int whence);
#定义_LARGEFILE64_SOURCE/*参见功能测试宏(7)*/
#包括
#包括
off64_t lseek64(内部fd、off64_t偏移、内部何处);

我现在手头没有MinGW,但是如果我没记错的话,有一个
\u stat64
函数使用了
结构。你可能想用一些狡猾的宏来隐藏这种丑陋

谢谢各位,好建议,但我想出来了。。。MinGW需要此定义来启用结构stat64和函数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; 
}

希望这对某人有所帮助。

您可以调用Windows API来获取打开句柄的文件大小,或者获取文件名的A/W


有关工作示例,请参见。

可能不需要太狡猾-只要在
fsize()
函数中使用
fsize()
就可以了。@MichaelBurr只要
fsize()
是他/她唯一使用的
stat
。(当我输入cunning时,我的舌头紧紧地贴在我的脸颊上!)这行得通,但_stat64仅在MinGW中可用,如果您在之前添加它35; include,否则stat64符号将无法解析:#define MSVCRT_VERSION 0x0601
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; 
}