问题描述
int filesize(FILE * handle)
{
int filesize;
int old_pointer = ftell(handle);
fseek(handle, 0, SEEK_END);
filesize = ftell(handle);
fseek(handle, old_pointer, SEEK_SET);
return filesize;
}
这是一个函数返回一个文件的大小?一个好办法
Is this a good way for a function to return the size of a file?
推荐答案
有这样做,单程只要你的文件是不是太大(对于32位系统还是Windows 64位,意味着不超过2吉布)。它具有的任何平台上或多或少工作的优点。当然,返回长
,而不是 INT
(所以64位非Windows系统上,则文件可以在报告中大得多,只要您解决您的code适当)。然而,需要四个功能调用是一件小事昂贵的。
It is one way to do it, as long as your files aren't too big (which, for a 32-bit system or Windows 64-bit, means not more than 2 GiB). It has the merit of more or less working on any platform. Of course, ftell()
returns a long
, not an int
(so on a 64-bit non-Windows system, the files it can report on are much bigger, as long as you fix your code appropriately). However, requiring four function calls is a trifle expensive.
POSIX的替代品,和。
The POSIX alternatives are stat()
, lstat()
and fstat()
.
有将Windows API中的类似物。
There will be analogues in the Windows API.
这篇关于函数返回文件的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!