Hi,

On Thu, 2012-11-08 at 19:25 +0100, Oliver Rath wrote:
> Ok, this is right, too. But I need an recursive access to a directory
> tree. Maybe there is a possibility to get the filetype (i only need type
> "dir" and "link")? It looks like there is no implementation in C++ or
> tntnet for it. Any suggestions for that? My first idea was to use the
> "chdir" method of cxxtools::directory, but this seems broken (what
> happens, if chdir fails?).
> 

chdir is just not what you want to do, it changes the working Directory
of the running Process. However, you can use a recusive lookup with
cxxtools::Directory, consider the following example (Remember, care for
exceptions, some subdirs might not be readable).


Regards,
Julian


#include <string>
#include <vector>
#include <iostream>

#include <cxxtools/fileinfo.h>
#include <cxxtools/directory.h>

void recusive_filelist(std::vector<std::string>& _vec,
cxxtools::FileInfo& _fi)
{
        cxxtools::Directory dir(_fi);
        for (cxxtools::DirectoryIterator it = dir.begin(); it !=
dir.end(); ++it)
        {
                cxxtools::FileInfo path(it.path());
                if(path.type() == cxxtools::FileInfo::Directory)
                {
                        if(path.name() != "." && path.name() != "..")
                                 recusive_filelist(_vec, path);
                }
                else
                        _vec.push_back(path.path());
        }
}

int main(void)
{
        std::vector<std::string> vec;
        cxxtools::FileInfo startdir("/opt");
        recusive_filelist(vec, startdir);

        for(std::vector<std::string>::const_iterator it = vec.begin();
it != vec.end();  it++)
                std::cout << "name: " << *it << "\n";

        return 0;
}



------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_nov
_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general

Reply via email to