Linux下struct dirent,DIR,struct stat使用例子
以下例子,读取遍历目录中的文件夹,文件,并输出一些属性值,具体的相关结构体定义,可以参考:
http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/
http://www.360doc.com/content/11/0110/16/4559801_85509847.shtml
/*
* linux struct dirent,DIR,struct stat
* date: 2015-7-16
* author: zhang
* compiled by gcc
*/
#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
int main(int argc,char **argv)
{
struct dirent *dirp;
struct stat buffer;
DIR *dp; //结构体,具体参阅http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/
if (argc != 2)
{
printf("argument is error!\n");
return 0;
}
if ((dp = opendir(argv[1])) == NULL)
{
printf("file path is error!\n");
return 0;
}
while(dirp = readdir(dp))
{
printf("filename: %s\n",dirp->d_name);//文件名
stat(dirp->d_name,&buffer);
// printf("%d\n",stat(dirp->d_name,&buffer));
printf("st_mode: %d\n",buffer.st_mode); //文件访问权限
printf("st_uid: %d\n",buffer.st_uid); //所有者用户识别号
printf("st_mtime: %d\n",buffer.st_mtime);//最后一次修改该文件的时间
printf("\n");
}
closedir(dp);
return 0;
}
代码地址:
https://github.com/zhangxinxing/some_code_in_csdn/blob/master/struct_dirent.c
参考文献:
http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/
http://www.360doc.com/content/11/0110/16/4559801_85509847.shtml
http://www.cplusplus.com/forum/unices/16005/