LAST UPDATED: MARCH 24, 2022
C++ Program To Display The List Of Current Directory/Folder Using File
In this tutorial, we will be learning how to display the list of current directory or folder using file.
C++ Program For Displaying The List of Current Directory/Folder Using File(First Method)
#include<iostream>
#include<dirent.h>
using namespace std;
int main()
{
struct dirent *d;
DIR *dr;
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;
}
closedir(dr);
}
else
cout<<"\nError Occurred!";
cout<<endl;
return 0;
}
C++ Program For Displaying The List of Current Directory/Folder Using File(Second Method)
This program does the same job as of previous program, but using while loop, instead of for loop.
#include<iostream>
#include<dirent.h>
using namespace std;
int main()
{
struct dirent *d;
DIR *dr;
dr = opendir(".");
if(dr!=NULL)
{
cout<<"List of Files and Folders:-\n";
while((d=readdir(dr))!=NULL)
cout<<d->d_name<<endl;
closedir(dr);
}
else
cout<<"\nError Occurred!";
cout<<endl;
return 0;
}
Conclusion
Here, in this tutorial, we have implemented the C++ program for displaying the list of current directory/folder using file.