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”将其打印出来。
5 个解决方案
#2
5
You can use the Win32 GetFileSizeEx
function.
您可以使用Win32 GetFileSizeEx函数。
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;
#3
3
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:
我(64位x86机器)上stat的手册页说:
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())path指的是无法在off_t类型中表示其大小的文件。在没有-D_FILE_OFFSET_BITS = 64的情况下在32位平台上编译的应用程序在大小超过(2 << 31)-1位的文件上调用stat()时,可能会发生这种情况。
#4
2
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
#5
0
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功能。然后从开始文件指针中减去结束文件指针。它还会为您提供文件大小。 (字节数)。
#1
#2
5
You can use the Win32 GetFileSizeEx
function.
您可以使用Win32 GetFileSizeEx函数。
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;
#3
3
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:
我(64位x86机器)上stat的手册页说:
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())path指的是无法在off_t类型中表示其大小的文件。在没有-D_FILE_OFFSET_BITS = 64的情况下在32位平台上编译的应用程序在大小超过(2 << 31)-1位的文件上调用stat()时,可能会发生这种情况。
#4
2
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
#5
0
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功能。然后从开始文件指针中减去结束文件指针。它还会为您提供文件大小。 (字节数)。