स्टैंडर्ड C++ ऐसा करने का कोई तरीका प्रदान नहीं करता। आप सिस्टम कमांड का उपयोग करके ls कमांड को इनिशियलाइज़ कर सकते हैं:
उदाहरण
#include<iostream> int main () { char command[50] = "ls -l"; system(command); return 0; }आउटपुट
यह आउटपुट देगा:
-rwxrwxrwx 1 root root 9728 Feb 25 20:51 a.out -rwxrwxrwx 1 root root 131 Feb 25 20:44 hello.cpp -rwxrwxrwx 1 root root 243 Sep 7 13:09 hello.py -rwxrwxrwx 1 root root 33198 Jan 7 11:42 hello.o drwxrwxrwx 0 root root 512 Oct 1 21:40 hydeout -rwxrwxrwx 1 root root 42 Oct 21 11:29 my_file.txt -rwxrwxrwx 1 root root 527 Oct 21 11:29 watch.py
यदि आप विंडोज पर हैं, तो सूची प्रदर्शित करने के लिए ls के बजाय dir का उपयोग कर सकते हैं।
उदाहरण
आप डायरेक्ट पैकेज (https://github.com/tronkko/dirent) का उपयोग करके अधिक लचीला API उपयोग कर सकते हैं। आप फ़ाइलों की सूची प्राप्त करने के लिए इसका उपयोग इस प्रकार कर सकते हैं:
#include <iostream> #include <dirent.h> #include <sys/types.h> using namespace std; void list_dir(const char *path) { struct dirent *entry; DIR *dir = opendir(path); if (dir == NULL) { return; } while ((entry = readdir(dir)) != NULL) { cout << entry->d_name << endl; } closedir(dir); } int main() { list_dir("/home/username/Documents"); }आउटपुट
यह आउटपुट देगा:
a.out hello.cpp hello.py hello.o hydeout my_file.txt watch.py