
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get List of Files in a Directory Using C or C++
Listing files in a directory is used to write a program that opens a specified folder (e.g: "/myfiles"), reads its contents, and displays the names of each file and subfolder one by one.
In C/C++, to see all the files in a directory, you can use special system functions that let you read the directory's contents.
In real life, we open folder to see the contents inside the files. Similarly, in C or C++, we can write a program to display all the files and folders in a directory.
Algorithm
Following is the algorithm to get the list of files in a directory using C/C++.
Begin Declare a poniter dr to the DIR type. Declare another pointer en of the dirent structure. Call opendir() function to open all file in present directory. Initialize dr pointer as dr = opendir("."). If(dr) while ((en = readdir(dr)) != NULL) print all the file name using en->d_name. call closedir() function to close the directory. End.
C/C++ Programs to Get List of Files in a Directory
To get list of files in a directory, we open the current folder and prints the names of all files inside it.
Let us assume that the current directory contains the following files and folders as main.cpp, notes.txt, images(folder) and data.csv.
Let us have an clear idea about the program step by step in C/C++:
- step 1: Open the directory using opendir().
- step 2: Read each entry (file/folder) using readdir() inside a loop.
- step 3: Print the name of each entry using en->d_name.
- step 4: Close the directory using closedir().
Example
This program opens the current folder and prints the names of all files and subfolders inside it, one by one.
#include <stdio.h> #include <dirent.h> int main(void) { DIR *dr; struct dirent *en; dr = opendir("."); //open all or present directory if (dr) { while ((en = readdir(dr)) != NULL) { printf("%s\n", en->d_name); //print all directory name } closedir(dr); //close all directory } return(0); }
If the files exists, the output will be
main.cpp notes.txt images data.csv
If the files doesn't exists(empty directory), the output will be
No such file or directory
#include <iostream> #include <dirent.h> #include <sys/types.h> using namespace std; int main(void) { DIR *dr; struct dirent *en; dr = opendir("."); //open all directory if (dr) { while ((en = readdir(dr)) != NULL) { cout<<" \n"<<en->d_name; //print all directory name } closedir(dr); //close all directory } return(0); }
If the files exists, the output will be
main.cpp notes.txt images data.csv
If the files doesn't exists(empty directory), the output will be
No such file or directory
C++ Program to List All Files in a Directory Using <filesystem>
In C++17, a new header called <filesystem> was introduced that makes the program easy to work with files and directories. Instead of using older system-based functions like opendir() and readdir().
Example
This program uses C++17's <filesystem> to open the current folder and print the name of each file and subfolder inside it.
#include<iostream> #include<filesystem> // C++17 feature namespace fs = std::filesystem; int main(){ std::string path = "."; // Current directory for(const auto&entry:fs::directory_iterator(path)) { std::cout<<entry.path().filename()<<std::endl; } return 0; }
If the files exists, the output will be
main.cpp notes.txt images data.csv
If the files doesn't exists(empty directory), the output will be
No such file or directory
This <filesystem> feature is only available in C++17 and does not exist in C. As it is not part of C, so for similar functionality in C, you have to rely on OS-specific methods.