does linux provide such a function? Thanks!
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!...
does linux provide such a function? Thanks!
on the command lineCode:du -sh dirname
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.
Next door down...
http://cboard.cprogramming.com/linux-programming/
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
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.
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