-1

Following is a code snippet I used to get directory size in c++:

  boost::system::error_code ec;
  boost::filesystem::space_info si =
    boost::filesystem::space(path, ec);
  if (ec.value() == 0) {
    cout << si.capacity - si.available;
  }

But the above snippet seems to be giving the entire disk size instead of a particular directory size on a disk since I passed 2 different directory paths to above but both of them gave the same answer. Can someone help me with finding what's wrong with this or give me another alternative for getting the directory size in c++? TIA!

Doraemon
  • 109
  • 1
  • 7

1 Answers1

4

Boost Reference states:

The value of the space_info object is determined as if by using ISO/IEC 9945 statvfs() to obtain an ISO/IEC 9945 struct statvfs.

Opengroup Reference states:

The statvfs() function shall obtain information about the file system containing the file named by path.

man page states:

The function statvfs() returns information about a mounted filesystem. path is the pathname of any file within the mounted filesystem.

Based on your comment:

Have a look at the example of std::filesystem::directory_entry::file_size
Or better: Overview of Filesystem library

If its still not clear:

Erdal Küçük
  • 4,810
  • 1
  • 6
  • 11
  • statsfs seems to be returning the entire disk space. I want something that gives me the size of a directory alone. Can you please help me with this. – Doraemon Jun 22 '21 at 12:43