Linux系统常用目录操作函数 分类: B3_LINUX 2013-02-18 16:44 486人阅读 评论(0) 收藏
2013-02-18 16:44
696 查看
参考《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标准目录配置(转自鸟哥) 分类: B3_LINUX 2012-10-14 16:56 620人阅读 评论(0) 收藏
- vi操作指令 分类: B3_LINUX 2012-10-18 22:35 446人阅读 评论(0) 收藏
- Linux 系统挂载数据盘 分类: B3_LINUX 2015-01-30 18:13 228人阅读 评论(0) 收藏
- Linux 最常用命令 分类: B3_LINUX 2012-10-13 11:23 663人阅读 评论(0) 收藏
- Linux系统信息查看命令大全 分类: arm-linux-Ubuntu 2013-07-22 16:28 302人阅读 评论(0) 收藏
- Linux负载均衡集群之LVS原理 分类: 系统架构 Linux 2015-05-27 22:16 61人阅读 评论(0) 收藏
- 使用ganglia监控hadoop及hbase集群 分类: B3_LINUX 2015-03-06 20:53 646人阅读 评论(0) 收藏
- python常用函数总结 分类: python基础学习 2014-02-07 14:12 260人阅读 评论(0) 收藏
- 系统学习深度学习(七)--主流深度学习开源框架对比 2017-01-23 11:05 2292人阅读 评论(0) 收藏 举报 分类: 深度学习(30) 目录(?)[+] 转自:http://b
- Android的eclipse常用操作详解ZZ 分类: Android开发 2014-05-30 10:54 62人阅读 评论(0) 收藏
- 修改Linux中的用户名 分类: B3_LINUX 2014-07-24 11:40 440人阅读 评论(0) 收藏
- Linux负载均衡集群之LVS原理 分类: 系统架构 Linux 2015-05-27 22:16 60人阅读 评论(0) 收藏
- Linux的proc文件系统 分类: linux 2014-06-02 10:21 623人阅读 评论(0) 收藏
- PHP从零单排(七)用PHP操作目录和文件 分类: PHP 2014-04-23 15:42 112人阅读 评论(0) 收藏
- Redis实战之征服 Redis + Jedis + Spring (三) 分类: 开源应用系统 2013-08-03 11:07 2136人阅读 评论(0) 收藏 举报 一开始以为Spring下操作
- ubuntu系统文件夹目录说明 分类: Ubuntu学习笔记 2015-04-13 21:21 49人阅读 评论(0) 收藏
- Linux中fork()函数详解 分类: Ubuntu学习笔记 2015-07-27 13:56 9人阅读 评论(0) 收藏
- linux如何查看系统信息 分类: ubuntu 2013-06-20 13:51 330人阅读 评论(0) 收藏
- sqoop 和mysql相关操作 分类: Linux hadoop 2015-06-02 22:42 38人阅读 评论(0) 收藏
- 公钥,私钥和数字签名这样最好理解 分类: B3_LINUX 2015-05-06 16:25 59人阅读 评论(0) 收藏