Display the Current Directory Using File

Beginner

Introduction

In this lab, you will learn how to write a C++ program to display the current directory or folder using file. You will implement two different methods to display the list of files and folders in the current directory.

Create a C++ File

Create a C++ file named main.cpp in the ~/project directory. You can use any code editor or IDE to create the file.

touch ~/project/main.cpp

Include Required Libraries

In this step, you need to include the required libraries for the program. These libraries are necessary to manipulate files and directories in C++. Use the following code to include the libraries:

#include<iostream>
#include<dirent.h> // Header file for directory operations
using namespace std;

Implement First Method

In this step, we will implement the first method to display the list of files and folders in the current directory. Use the following code to display the list using a for loop:

int main()
{
    struct dirent *d; // Pointer for directory entry
    DIR *dr; // Pointer for directory
    dr = opendir(".");
    if(dr!=NULL)
    {
        cout<<"List of Files & Folders:\n";
        for(d=readdir(dr); d!=NULL; d=readdir(dr))
        {
            cout<<d->d_name<<endl; // Print the name of the file or folder
        }
        closedir(dr);
    }
    else
        cout<<"\nError Occurred!";
    cout<<endl;
    return 0;
}

Implement Second Method

In this step, we will implement the second method to display the list of files and folders in the current directory. Use the following code to display the list using a while loop:

int main()
{
    struct dirent *d; // Pointer for directory entry
    DIR *dr; // Pointer for directory
    dr = opendir(".");
    if(dr!=NULL)
    {
        cout<<"List of Files & Folders:\n";
        while((d=readdir(dr))!=NULL)
            cout<<d->d_name<<endl; // Print the name of the file or folder
        closedir(dr);
    }
    else
        cout<<"\nError Occurred!";
    cout<<endl;
    return 0;
}

Compile and Run the Program

In this step, you need to compile and run the main.cpp file. Use the following command to compile and run the program:

g++ main.cpp -o main && ./main

Full Code of main.cpp

#include<iostream>
#include<dirent.h> // Header file for directory operations
using namespace std;

// Method 1
int main()
{
    struct dirent *d; // Pointer for directory entry
    DIR *dr; // Pointer for directory
    dr = opendir(".");
    if(dr!=NULL)
    {
        cout<<"List of Files & Folders:\n";
        for(d=readdir(dr); d!=NULL; d=readdir(dr))
        {
            cout<<d->d_name<<endl; // Print the name of the file or folder
        }
        closedir(dr);
    }
    else
        cout<<"\nError Occurred!";
    cout<<endl;

    // Method 2
    dr = opendir(".");
    if(dr!=NULL)
    {
        cout<<"List of Files & Folders:\n";
        while((d=readdir(dr))!=NULL)
            cout<<d->d_name<<endl; // Print the name of the file or folder
        closedir(dr);
    }
    else
        cout<<"\nError Occurred!";
    cout<<endl;
    return 0;
}

Summary

In this lab, you have learned how to write a C++ program to display the current directory or folder using file. You have implemented two different methods to display the list of files and folders in the current directory. You have also learned how to compile and run a C++ program in Ubuntu terminal.

Other Tutorials you may like