Boost makes this simple using it's filesystem library iterators and a very simple loop construct of your choice.
Example
This example shows a recursive walk of a folder structure printing the full path of all folders and files found.#include <boost/filesystem.hpp>
#include <iostream>
void main()
{
boost::filesystem::path path = boost::filesystem::current_path();
boost::filesystem::recursive_directory_iterator itr(path);
while (itr != boost::filesystem::recursive_directory_iterator())
{
std::cout << itr->path().string() << std::endl;
++itr;
}
}
- Firstly we assume you have built the Boost filesystem library and it and it's header are appropriately defined in your project e.g. link to the library and include the header file.
- Next you create the iterator recursive_directory_iterator. We have loaded our iterator example with the current working directory.
- Finally you loop until you run out of iterators.
How do you tell boost to list the files in alpha order?
ReplyDeleteie:
a1
a2
b
b3
xxxx
etc
I believe you can use the std::sort.
DeleteSo for example if you have a vertor path you could use the begin and end iterators to sort them.
Something like this (not tested)
std::vector < path > myfolders;
// fill the vector with folders from where ever
std::sort(myfolders.begin(), myfolders.end();
look here:
http://www.boost.org/doc/libs/1_47_0/libs/filesystem/v3/doc/tutorial.html#Using-path-decomposition
C language is a computer programming language.Very easy to learn and a strutured language. C language is a object oriented programming.
ReplyDeleteC++ training in chennai|Unix training in Chennai | FITA Velachery Reviews
@Dhivya Shree Well C is not an object oriented language at all I am afraid. It is a procedural language. I assume this is a typo.
ReplyDeletehttps://en.wikipedia.org/wiki/C_(programming_language)