Program to List Files in Directory

CCBeginner
Practice Now

Introduction

In this lab, we will create a C program to list all the files present in a directory using the dirent.h header file.

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/CompoundTypesGroup(["`Compound Types`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/ControlFlowGroup -.-> c/while_loop("`While Loop`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/CompoundTypesGroup -.-> c/structures("`Structures`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123315{{"`Program to List Files in Directory`"}} c/comments -.-> lab-123315{{"`Program to List Files in Directory`"}} c/variables -.-> lab-123315{{"`Program to List Files in Directory`"}} c/data_types -.-> lab-123315{{"`Program to List Files in Directory`"}} c/operators -.-> lab-123315{{"`Program to List Files in Directory`"}} c/if_else -.-> lab-123315{{"`Program to List Files in Directory`"}} c/while_loop -.-> lab-123315{{"`Program to List Files in Directory`"}} c/pointers -.-> lab-123315{{"`Program to List Files in Directory`"}} c/structures -.-> lab-123315{{"`Program to List Files in Directory`"}} c/function_parameters -.-> lab-123315{{"`Program to List Files in Directory`"}} c/function_declaration -.-> lab-123315{{"`Program to List Files in Directory`"}} end

Include Header Files

We need to include the standard input/output and dirent header files by writing the following code at the beginning of the program:

#include <stdio.h>
#include <dirent.h>

Define Main Function

The main() function is the entry point of our program. Our program starts executing from here. We will also declare the directory pointer d of type DIR and directory entry pointer dir of type struct dirent.

int main(void)
{
    DIR *d;
    struct dirent *dir;
    /*Your code goes here*/
    return 0;
}

Open the Directory

We will open the required directory using the opendir() function. Here, the dot (.) operator indicates the current directory.

d = opendir(".");

Read the Directory

We will read each entry of the directory using the readdir() function. Here, we check that the directory pointer is not NULL. If it is not NULL, we will print all the files present in the directory.

if (d)
{
    while ((dir = readdir(d)) != NULL)
    {
        printf("%s\n", dir->d_name);
    }
    closedir(d);
}

Full Code

Here is the full code of the program:

#include<stdio.h>
#include<dirent.h>

int main(void)
{
    DIR *d;
    struct dirent *dir;
    d = opendir(".");
    if (d)
    {
        while ((dir = readdir(d)) != NULL)
        {
            printf("%s\n", dir->d_name);
        }
        closedir(d);
    }
    return(0);
}

Summary

In this lab, we have learned how to create a C program to list the names of all the files in a directory. We have used functions from the dirent.h header file to achieve this. By using this program, we can easily retrieve the names of all the files present in the specified directory.

Other C Tutorials you may like