C/C++ : Get List Files in Directory

I have a problem when want to get list of files in directory or get list of directory and subdirectory using C++. So, I have try to research this problem and get a simple method using dirent library. Please check below code to get list of files in directory or get list of directory and subdirectory using C++.

Create file with name list_file.cpp. Copy code below :

Compile code to get list of files in directory or get list of directory and subdirectory using C++ using command :

if you set listFiles function in second argument to false, we can get only list of files and directory in current directory. Example, the output from my computer is like this (setting argument to false)

But, if you set the second argument to true, we will get list of files and directory in sub directory. I hope this code can help you to get list of files in directory or get list of directory and subdirectory using C++.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.