Studytonight is now part of the GUVI universe. Explore GUVI →
🤩 New Cool Developer Tools for you. Explore →
FREE JavaScript Video Series Start Learning →
Signup/Sign In
Tests
MCQs to test your knowledge.
Compilers
Compilers to execute code in browser.
Index
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.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.

Learn to Code
Learn and practice coding side-by-side.
NEW
C language Course
115+ coding exercises
Javascript Course
85+ coding exercises

AltStyle によって変換されたページ (->オリジナル) /