Linux系统常用目录操作函数
2015-06-17 10:23
190 查看
参考《Linux程序设计》第二版P103
扫描目录:
涉及的几个目录操作函数为:
(1) DIR *opendir(const char *name);
(2) struct dirent *readdir(DIR *dirp);
(3) int closedir(DIR *dirp);
(4) int chdir(const char *path);
以及文件操作函数:
int lstat(const char *path, struct stat *buf);
扫描目录:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <dirent.h> #include <unistd.h> #include <sys/stat.h> void printdir(char *dir, int depth); int main(void){ printf("Directory scan of /home:\n"); printdir("/home", 0); printf("Done.\n"); exit(0); } void printdir(char *dir, int depth){ DIR *dp; struct dirent *entry; struct stat statbuf; if((dp = opendir(dir)) == NULL){ fprintf(stderr, "Can not open directory: %s.\n", dir); return; } chdir(dir); while((entry = readdir(dp)) != NULL){ lstat(entry->d_name, &statbuf); if(S_ISDIR(statbuf.st_mode)){ if((strcmp(".", entry->d_name)==0)||(strcmp("..", entry->d_name) == 0)){ continue; } printf("%*s%s/\n", depth, "", entry->d_name); printdir(entry->d_name, depth+4); } else{ printf("%*s%s\n", depth, "", entry->d_name); } } chdir(".."); closedir(dp); }
涉及的几个目录操作函数为:
(1) DIR *opendir(const char *name);
(2) struct dirent *readdir(DIR *dirp);
(3) int closedir(DIR *dirp);
(4) int chdir(const char *path);
以及文件操作函数:
int lstat(const char *path, struct stat *buf);
相关文章推荐
- 完美图解教程 Linux环境VNC服务安装、配置与使用
- Linux学习路径
- Segment fault及LINUX core dump详解
- Linux中,创建聊天服务器、移除冗余软件包的实用命令
- Linux 最常用命令
- Linux标准目录配置(转自鸟哥)
- Zabbix日志监控:Linux异常登录告警
- linux下chown,chgrp,chmod解析
- CentOS主菜单编辑工具
- Java Too many open files Linux
- linux下mysql开启远程访问权限及防火墙开放3306端口
- centos 清除root密码
- 解决HP服务器安装Centos7 x64无法识别硬盘
- Linux基础(四):文本编辑器Vim
- Linux cpuidle framework(1)_概述和软件架构
- Linux中7个用来浏览网页和下载文件的命令
- VxWorks的环形缓冲区和linux的环形缓冲区的区别
- Linux定时任务Crontab详解
- Linux脚本中用户自定义终止符-<<EOF
- 详解Linux中SSH远程访问控制