如何在 c++ 中确定大型二进制文件的大小
How to determine size of a huge binary file in c++
要确定二进制文件的大小,似乎总是涉及将整个文件读入内存。如何确定已知比内存可以占用的非常大的二进制文件的大小?
在大多数系统上,有stat()
和 fstat()
功能(不是ANSI-C的一部分,而是POSIX的一部分)。对于 Linux,请查看手册页。
编辑:对于Windows,文档在这里。
编辑:对于更便携的版本,请使用Boost库:
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: tut1 pathn";
return 1;
}
std::cout << argv[1] << " " << file_size(argv[1]) << 'n';
return 0;
}
#include <cstdio>
FILE *fp = std::fopen("filename", "rb");
std::fseek(fp, 0, SEEK_END);
long filesize = std::ftell(fp);
std::fclose(fp);
或者,使用 ifstream
:
#include <fstream>
std::ifstream fstrm("filename", ios_base::in | ios_base::binary);
fstrm.seekg(0, ios_base::end);
long filesize = fstrm.tellg();
这应该有效:
uintmax_t file_size(std::string path) {
return std::ifstream(path, std::ios::binary|std::ios::ate).tellg();
}
相关文章:
- 正在读取二进制文件(is_open)
- 在C++中将类(带有Vector成员)保存为二进制文件
- 如何从二进制文件中读取字符串
- 保存/加载大量短数组到二进制文件
- 从二进制文件中读取整数数组
- Android 在编译二进制文件时重建静态库
- 将大型二进制字符串写入二进制文件
- 在 C++ 优化中合并大型二进制文件
- 如何将大型二进制文件(10000k 位)转换为基数 3 或基数 31?
- 具有标准C++的大型二进制文件副本
- 无法将大型无符号整数正确写入二进制文件 - C++
- 在执行期间将大型多维数组读取和写入二进制文件并返回数组
- 大型二进制文件添加
- ofstream::write 在写入大型二进制文件时中间失败
- 使用 ifstream 剂量读取大型二进制文件不会填充字符缓冲区C++
- 使用datastax c++驱动程序将大型二进制文件或数组(超过64MB)插入Cassandra
- 编辑大型二进制文件
- 解析/跳转具有大小元素的大型二进制文件的最快方法
- 可以打开小型ASCII文件,但不能打开大型二进制文件
- 如何在 c++ 中确定大型二进制文件的大小