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.
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP