C/C++:遍历目录
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main(int ac, char *av[])
{
DIR *dir = NULL;
struct dirent *item = NULL;
dir = opendir(av[1]);
if (dir == NULL)
printf("opendir error\n");
while ((item = readdir(dir)) != NULL)
printf("item.name : %s\n", item->d_name);
closedir(dir);
return 0;
}
[test1280@localhost ~]$ !g
gcc -o main main.c
[test1280@localhost ~]$ ./main .
item.name : 20170605
item.name : 20170522
item.name : SR200550
item.name : main
item.name : rsa.json
item.name : .emacs
……
Keys:
DIR *dir;
struct dirent *item;
dir = opendir(xxx);
item = readdir(dir);
item->d_name;
closedir(dir);