Is there a way to get a linux directory size in c++?

This is a discussion on Is there a way to get a linux directory size in c++? within the Linux Programming forums, part of the Platform Specific Boards category; does linux provide such a function? Thanks!...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Is there a way to get a linux directory size in c++?

    does linux provide such a function? Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Code:
    du -sh dirname
    on the command line

    I don't think it's possible to do it directly in C++. You will probably have to write a recursive function to do it.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by meili100 View Post
    does linux provide such a function? Thanks!
    Next door down...

    http://cboard.cprogramming.com/linux-programming/

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,272
    Moved to Linux Programming.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    get all the files in the dir
    do stat() on each file
    accumulate the size
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And use Boost.Filesystem's recursive_directory_iterator to do it. For example, if you're not worried about symlinks, you can do this:
    Code:
    #include <boost/cstdint.hpp>
    #include <algorithm>
    #include <boost/filesystem.hpp>
    #include <boost/bind.hpp>
    #include <boost/iterators/transform_iterator.hpp>
    namespace fs = boost::filesystem;
    
    boost::uintmax_t dir_size(const fs::path &p)
    {
      return std::accumulate(
        boost::make_transform_iterator(&fs::file_size, fs::recursive_directory_iterator(p)),
        boost::make_transform_iterator(&fs::file_size, fs::recursive_directory_iterator()),
        static_cast<boost::uintmax_t>(0));
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 08:32 PM
  2. Linux Linux why Linux??
    By afreedboy in forum Tech Board
    Replies: 146
    Last Post: 01-21-2004, 06:27 PM
  3. Replies: 11
    Last Post: 03-25-2003, 05:13 PM
  4. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 07:45 PM
  5. total size of a directory using C / C++
    By metthew in forum C Programming
    Replies: 1
    Last Post: 08-01-2002, 07:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21