windows 如何获取大于 4 Gb 的文件大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2361385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a file's size which is greater than 4 Gb?
提问by kampi
I made a simple function, which get's a file's size.
我做了一个简单的函数,它得到一个文件的大小。
int file_size( char * filename )
{
int size;
struct stat st;
stat( filename, &st );
size = st.st_size;
return size;
}//file_size
It is working fine, but if i have a file which size is bigger than 4Gb than i get a negativ number back, and of course this isn't the correct file size. So how can i get such a big file's size? I think, that the return value should anything else like int but i don't know what, and i don't think, that would solve my problem.
它工作正常,但如果我有一个大于 4Gb 的文件,我会得到一个负数,当然这不是正确的文件大小。那么我怎样才能得到这么大的文件呢?我认为,返回值应该是 int 之类的任何东西,但我不知道是什么,而且我认为这不会解决我的问题。
Thanks,
谢谢,
kampi
坎皮
Update:
更新:
Hi!
你好!
I found the solution. I have to use __stat64. I modifyed my function, and now it is retrieving the real size. I tested it with an 8Gb big file.
我找到了解决方案。我必须使用 __stat64。我修改了我的函数,现在它正在检索实际大小。我用 8Gb 大文件对其进行了测试。
unsigned long long int file_size( char * filename )
{
unsigned long long int size;
struct __stat64 st;
__stat64( filename, &st );
size = st.st_size;
return size;
}//file_size
And a notice:
还有一个通知:
When i used printf, i had to use "%I64d" to print it out.
当我使用 printf 时,我不得不使用“%I64d”来打印它。
回答by Kerido
You can use the Win32 GetFileSizeEx
function.
您可以使用 Win32GetFileSizeEx
函数。
HANDLE aFile = CreateFile
(
filename,
FILE_READ_ATTRIBUTES,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (aFile != INVALID_HANDLE_VALUE)
{
long long aSize;
GetFileSizeEx(aFile, &aSize);
CloseHandle(aFile);
return aSize;
}
else
return -1;
回答by YOU
st_size
is defined as long in wchar.h, so you could probably try with long, instead of int
st_size
在 wchar.h 中定义为 long,因此您可以尝试使用long,而不是 int
struct stat {
....
_off_t st_size;
....
}
....
typedef long _off_t;
May be like this
可能是这样的
long file_size( char * filename )
{
long size;
struct stat st;
stat( filename, &st );
size = st.st_size;
return size;
}//file_size
回答by Arun
In such cases, return value of stat()
might be -1, and errno
set to EOVERFLOW
.
在这种情况下, 的返回值stat()
可能是 -1,并errno
设置为EOVERFLOW
。
The man page for stat
on my (64 bit x86 machine) says:
stat
我的(64 位 x86 机器)上的手册页说:
EOVERFLOW
(stat()) path refers to a file whose size cannot be represented in the type off_t. This can occur when an application compiled on a 32-bit platform without -D_FILE_OFFSET_BITS=64 calls stat() on a file whose size exceeds (2<<31)-1 bits.
溢流
(stat()) 路径指的是大小不能用 off_t 类型表示的文件。当在没有 -D_FILE_OFFSET_BITS=64 的 32 位平台上编译的应用程序对大小超过 (2<<31)-1 位的文件调用 stat() 时,可能会发生这种情况。
回答by Pavunkumar
I think , problem is because of file size more than INT_MAX , So that it is giving you negative values .
我认为,问题是因为文件大小超过 INT_MAX,所以它给你负值。
Probably you can do type cast for long data type , it may work.
可能您可以对长数据类型进行类型转换,它可能会起作用。
Otherwise use , fseek , ftell function . then subtract end file pointer from start file pointer . It will also give you file size. ( number of bytes ) .
否则使用、fseek、ftell 函数。然后从开始文件指针减去结束文件指针。它还会为您提供文件大小。(字节数)。