如何在C ++中获取文件大小? [英] How can I get a file's size in C++?

查看:487
本文介绍了如何在C ++中获取文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们创建一个补充性问题,以这一个
在C ++中获取文件大小的最常见方法是什么?
在回答之前,确保它是可移植的(可以在Unix,Mac和Windows上执行),
可靠,易于理解,没有库依赖(没有boost或qt,但是例如glib是确定的它是可移植库)。

Let's create a complementary question to this one. What is the most common way to get the file size in C++? Before answering, make sure it is portable (may be executed on Unix, Mac and Windows), reliable, easy to understand and without library dependencies (no boost or qt, but for instance glib is ok since it is portable library).

推荐答案

#include <fstream>

std::ifstream::pos_type filesize(const char* filename)
{
    std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
    return in.tellg(); 
}

查看 http://www.cplusplus.com/doc/tutorial/files/ 了解有关C ++文件的更多信息。

See http://www.cplusplus.com/doc/tutorial/files/ for more information on files in C++.

这篇关于如何在C ++中获取文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆