关于如何测量文件大小的几个主题(请参阅Using C++ filestreams (fstream), how can you can determine the size of a file?和C++: Getting wrong file size)计算文件开头和结尾之间的差异:
std::streampos fileSize( const char* filePath ){
std::streampos fsize = 0;
std::ifstream file( filePath, std::ios::binary );
fsize = file.tellg();
file.seekg( 0, std::ios::end );
fsize = file.tellg() - fsize;
file.close();
return fsize;
}
但不是在开始时打开文件,我们可以在最后打开它并采取措施,如下所示:
std::streampos fileSize( const char* filePath ){
std::ifstream file( filePath, std::ios::ate | std::ios::binary );
std::streampos fsize = file.tellg();
file.close();
return fsize;
}
它会起作用吗?如果不是为什么?