调用opendir,readdir,stat函数
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
int main()
{
DIR *drip = opendir("请在此处输入路径");//例如:/mnt/hgfs/share/mycode/
if(drip == NULL)
{
perror("目录获取失败,原因是");
}
struct dirent *p;
long int sum = 0;
while(1)
{
p = readdir(drip);
if(p == NULL)
{
break;
}
struct stat sb;
int s = stat(p->d_name,&sb);
if(-1 == s)
{
perror("文件属性获取失败,原因是");
return -1;
}
if ((sb.st_mode & S_IFMT) == S_IFREG)
{
sum+=sb.st_size;
printf("%s文件,文件属性为普通文件,文件有%ld个字节,此时普通文件总字节为%ld\n",p->d_name,sb.st_size,sum);
}
else
{
printf("%s文件,文件属性为非普通文件,文件有%ld个字节,不计入\n",p->d_name,sb.st_size);
}
}
closedir(drip);
return 0;
}