说明
代码
#include <stdio.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
DIR *dp;
struct dirent *dirstruct;
if(argc <=1 )
{
printf("参数太少了老铁\n");
return 0;
}
dp = opendir(argv[1]);
if(dp == NULL)
{
fprintf(stderr, "打开%s目录失败:%s\n", argv[1], strerror(errno));
return -1;
}
while(1)
{
dirstruct = readdir(dp);//每次读取得到文件夹当中的一个文件的信息
if(dirstruct == NULL)//读取结束
break;
switch(dirstruct->d_type)
{
case DT_BLK:
printf("块设备文件\n");
break;
case DT_REG:
printf("普通文件\n");
break;
case DT_DIR:
printf("文件夹/目录文件\n");
break;
}
printf("名字:%s\n", dirstruct->d_name);
}
closedir(dp);
return 0;
}