C 获取目录下所有文件和目录

#include <io.h>
#include <stdio.h>

#define MAX_LEN 4096

int main(void)
{
char root[] = “D:\\”;
struct _finddata_t file;
intptr_t hFile;
char buf[MAX_LEN];

if (_chdir(root))
{
printf(“打开文件夹失败: %s\n”, root);
return 1;
}

hFile = _findfirst(“*.*”, &file);
while (_findnext(hFile, &file) == 0)
{
sprintf_s(buf, MAX_LEN, “%s\%s”, root, file.name);
printf(“%s\n”, buf);
}
return 0;
}