linux環境下掃描文件
来源:互联网 发布:台州五轴编程工资 编辑:程序博客网 时间:2024/06/05 15:04
#include <fcntl.h> //open()函數#include <unistd.h>#include <stdio.h>#include <dirent.h> //提供目錄流操作#include <string.h>#include <sys/stat.h> //提供屬性操作函數#include <sys/types.h> //提供mode_t類型#include <stdlib.h>/** * 該函數的作用是遍歷目錄 * @dir 目錄 * @dePth 子目錄前增加空格的數量 * * * eg: /home * ./xiaoming * ./Downloads * ./Desktop * ./6140 * ./root *1. opendir("/home") 返回子目錄流 ( DIR* ) ./xiaoming 和 ./root *2. chdir("/home") 切換到/home目錄 *3. readdir(DIR*) 返回指定目錄的下一級信息( dirent* ) 如 ./Downloads ./Desktop *4. lstat("/home/xiaoming/Downloads", &statbuf); 獲取指定目錄的屬性 *5. chdir(".."); 回到上一級目錄,對應步驟2 *6. closedir("/home") 關閉目錄 */void scanDir(char* dir, int dePth) {DIR* dp; //定義子目錄流指針struct dirent* entry;//定義dirent結構體指針保存後續目錄struct stat statbuf; //定義statbuf結構體保存文件屬性if ((dp = opendir(dir)) == NULL) { //打開目錄,獲得子目錄流指針,判斷操作是否成功puts("無法打開該目錄");return;}chdir(dir); //切換到當前目錄去while ((entry = readdir(dp)) != NULL) { //獲得下一級目錄信息,如果爲結束則循環lstat(entry->d_name, &statbuf); //獲取下一級成員屬性if (S_IFDIR & statbuf.st_mode) { //判斷下一級成員是否是目錄if (strcmp(".", entry->d_name) == 0|| strcmp("..", entry->d_name) == 0) {//如果獲得的成員符號是 "." 或者 "..",則跳過本次循環continue;}printf("%*s%s/\n",dePth,"",entry->d_name);scanDir(entry->d_name,dePth+4); //遞歸,掃描下一級目錄}else{printf("%*s%s/\n",dePth,"",entry->d_name); //輸出屬性不是目錄的成員}}chdir(".."); //回到上一級目錄closedir(dp); //關閉子目錄}
阅读全文
0 0
- linux環境下掃描文件
- linux文件
- Linux 文件
- Linux文件
- linux文件
- linux文件
- Linux文件
- Linux文件
- Linux 文件
- linux文件
- linux 文件
- Linux文件
- Linux文件
- linux 文件
- Linux 文件
- Linux文件
- linux 文件
- linux文件
- Angular启动过程介绍
- shell控制结构语句
- PHP 暂停函数 sleep() 与 usleep() 的秒与微秒区别
- Unity引擎 特性(Attribute)组件使用说明
- UFLDL(2)Vectorized implementation
- linux環境下掃描文件
- linux GCC与Vi编辑器
- Angular项目目录介绍
- 图像识别
- iOS应用审核
- Thymeleaf系列四 生成URL地址和表达式工具对象
- 海量数据高并发的访问技术解决方案
- leetcode287. Find the Duplicate Number
- WebSocket + Spring消息推送服务的快速实现