Нужно рекурсивно обойти каталоги в Linux системе и вывести все файлы
Рекурсивный обход каталогов в Linux(С/С++)
2
Спросил
Лучший ответ:
2
Новые ответы
лев хасис лев хасис1
Можно использовать кроссплатформенную библиотеку Boost.Filesystem для работы с путями, файлами и директориями
Следующая функция получает путь к директории и имя файла и рекурсивно ищет в директории и поддиректории этот файл и возвращае булевую переменную, если true
то файл найден
bool find_file( const path & dir_path, // in this directory,
const std::string & file_name, // search for this name,
path & path_found ) // placing path here if found
{
if ( !exists( dir_path ) ) return false;
directory_iterator end_itr; // default construction yields past-the-end
for ( directory_iterator itr( dir_path );
itr != end_itr;
++itr )
{
if ( is_directory(itr->status()) )
{
if ( find_file( itr->path(), file_name, path_found ) ) return true;
}
else if ( itr->leaf() == file_name ) // see below
{
path_found = itr->path();
return true;
}
}
return false;
}