How can I get a file's size in C++? [duplicate

2019-02-23 00:51发布

This question already has an answer here:

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).

标签: c++ filesize
7条回答
够拽才男人
2楼-- · 2019-02-23 01:42

Using the C++ filesystem TS:

#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;

int main(int argc, char *argv[]) {
  fs::path p{argv[1]};
  p = fs::canonical(p);

  std::cout << "The size of " << p.u8string() << " is " <<
      fs::file_size(p) << " bytes.\n";
}
查看更多
登录 后发表回答