C 程式來列出目錄中所有檔案和子目錄
我們這裡給出了一個目錄。我們的任務是建立一個 C 程式,用於列出目錄中的所有檔案和子目錄。
目錄是一個存放一組檔案的位置/區域/地點。
子目錄是根目錄中的目錄,而它又可以包含另一個子目錄。
在 C 程式語言中,你可以輕鬆列出目錄中的所有檔案和子目錄。以下程式將演示如何在目錄中列出所有檔案和子目錄。
//C 程式來列出目錄中所有檔案和子目錄
示例
#include <stdio.h> #include <dirent.h> int main(void){ struct dirent *files; DIR *dir = opendir("."); if (dir == NULL){ printf("Directory cannot be opened!" ); return 0; } while ((files = readdir(dir)) != NULL) printf("%s
", files->d_name); closedir(dir); return 0; }
輸出
cprograms .. prog1.c prog2.c prog3.c ... prog41.c This will return all files and sub-directory of the current directory.
廣告