
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
How can I get the list of files in a directory using C or C++?
Let us consider the following C++ sample code to get the list of files in a directory.
Algorithm
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.
Example
#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); }
Output
BINSEARC.C BINTREE (1).C BINTREE.C BTREE.C BUBBLE.C c.txt file3.txt HEAP.C HEAPSORT.C HLINKLST.C INSERTIO.C LINKLIST.C LINKLST.C LLIST1.C players.cpp PolarRect.cpp QUEUE.C
Example
#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); }
Output
BINSEARC.C BINTREE (1).C BINTREE.C BTREE.C BUBBLE.C c.txt file3.txt HEAP.C HEAPSORT.C HLINKLST.C INSERTIO.C LINKLIST.C LINKLST.C LLIST1.C
- Related Articles
- How can I get the list of files in a directory using C/C++?
- How do I list all files of a directory in Python?
- How to get the list of jpg files in a directory in Java?
- How to list down all the files available in a directory using C#?
- How to list all files in a directory using Java?
- How can I list the contents of a directory in Python?
- Golang Program to get the list the name of files in a specified directory
- How can I iterate over files in a given directory in Python?
- How to list out the hidden files in a Directory using Java program?
- How to list all files (only) from a directory using Java?
- How to get all the files, sub files and their size inside a directory in C#?
- Using SAP ABAP, how can I read content of CSV files in a directory to an internal table?
- How to list the hidden files in a directory in Java?
- How can I create directory tree using C++ in Linux?
- How to copy a file, group of files, or directory in Linux?

Advertisements