- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C Program to list all files and sub-directories in a directory
Here, we are given a directory. Our task is to create a C program to list all files and sub-directories in a directory.
The directory is a place/area/location where a set of the file(s) will be stored.
Subdirectory is a directory inside the root directory, in turn, it can have another sub-directory in it.
In C programming language you can list all files and sub-directories of a directory easily. The below program will illustrate how to list all files and sub-directories in a directory.
//C program to list all files and sub-directories in a directory
Example
#include <stdio.h> #include <dirent.h> int main(void){ struct dirent *files; DIR *dir = opendir("."); if (dir == NULL){ printf("Directory cannot be opened!" ); return 0; } while ((files = readdir(dir)) != NULL) printf("%s
", files->d_name); closedir(dir); return 0; }
Output
cprograms .. prog1.c prog2.c prog3.c ... prog41.c This will return all files and sub-directory of the current directory.
- Related Articles
- Java program to List all files in a directory and nested sub-directory - Recursive approach
- How to get a list of all sub-directories in the current directory using Python?
- How to get all the files, sub files and their size inside a directory in C#?
- Java program to List all files in a directory recursively
- How to get all the directories and sub directories inside a path in C#?
- How to list all files in a directory using Java?
- How to list down all the files available in a directory using C#?
- Java program to delete all the files in a directory recursively (only files)
- How to list all files (only) from a directory using Java?
- How do I list all files of a directory in Python?
- Java program to merge contents of all the files in a directory
- How to list non-hidden files and directories in windows using Python?
- How to list out the hidden files in a Directory using Java program?
- Listing out directories and files using C#
- PHP: Unlink All Files Within A Directory, and then Deleting That Directory

Advertisements