Linux 系统编程--查看目录文件
来源:互联网 发布:淘宝店铺的模板怎么做 编辑:程序博客网 时间:2024/06/06 03:38
1、打印目录下所有文件及文件夹
#include <stdio.h>#include <unistd.h>#include <errno.h>#include <sys/stat.h>#include <sys/types.h>#include <dirent.h>int main(int argc,char **argv){ DIR* pDir; struct dirent* pDirInfo; if (argc == 2) { if (0 != chdir(argv[1])) { printf ("error num is : %d\n",errno); switch(errno) { case EACCES: printf("search permission is denied for one of the components of path\n"); break; case EIO: printf ("I/O error occurred\n"); break; case ENOENT: printf("No such file or directory\n"); break; case EPERM: printf ("Operation not permitted\n"); break; } return errno; } pDir = opendir(argv[1]); } else { pDir = opendir("."); } //打印当前目录 printf ("current directory is : %s\n",getcwd(NULL,0)); if (NULL == pDir) { perror("open dir failed !"); return -1; } pDirInfo = readdir(pDir); while (pDirInfo) { printf("--------------------------------\n"); printf("name: %s \n",pDirInfo->d_name); pDirInfo = readdir(pDir); //遍历文件夹 } closedir(pDir); return 0;}
2、遍历文件夹内容,包括子文件夹
//以树形结构的形式打印指定目录下的所有文件#include <unistd.h>#include <stdio.h>#include <dirent.h>#include <string.h>#include <sys/stat.h>#include <stdlib.h>#include <errno.h>void printDir1(const char * path, unsigned int depth);int main(int argc, char * *argv){ char *topDir = getcwd(NULL, 0); if (argc == 2) { topDir = argv[1]; } printf("遍历的目录为: %s\n", topDir); printDir1(topDir, 0); return 0;}void printDir1(const char * path, unsigned int depth){ DIR *pDir = opendir(path); if (NULL == pDir) { perror("路径打开失败!"); return; } struct dirent *entry; struct stat statbuf; chdir(path); char *curPath = getcwd(NULL,0); if (!curPath) { perror("curPath == NULL "); return; } while (NULL != (entry = readdir(pDir))) { stat(entry->d_name,&statbuf); char *desPath = (char*)malloc(strlen(curPath) + strlen(entry->d_name) + 2); sprintf(desPath,"%s/%s",curPath,entry->d_name); if (S_ISDIR(statbuf.st_mode)) { if ((strcmp(".",entry->d_name) == 0) || (strcmp("..",entry->d_name) == 0)) continue; printf("%d级--这是目录: %s\n",depth,desPath); printDir1(desPath,depth+1); } else { printf("%d级--这是文件: %s\n",depth,desPath); } free (desPath); } chdir(".."); closedir(pDir); printf("\t*****%d级目录%s--遍历完成**********\n",depth,path);}
阅读全文
0 0
- Linux 系统编程--查看目录文件
- linux 系统,查看某目录下有多少个文件
- Linux - 文件目录查看命令
- Linux查看目录中的文件
- linux查看文件/目录大小
- Linux系统文件目录
- Linux系统文件目录
- linux系统--文件目录
- Linux系统编程学习之《文件和目录操作》
- Linux系统编程---实现目录或者文件拷贝
- Linux系统下查看目录大小
- Linux系统下查看目录大小
- Linux系统下查看目录大小
- Linux系统下查看目录大小
- Linux系统下查看目录大小
- Linux系统下查看目录大小
- Linux系统下查看目录大小
- Linux系统文件目录简介
- 给定机器人坐标和朝向,判断某点在机器人的哪个方位
- Tomcat中404/500 错误,自定义错误页面
- 树莓派Android Things物联网开发:已知Bug发行说明
- 批量解压当前目录下的zip文件_循环执行
- PAT (Basic Level) Practise (中文) 1044. 火星数字(20)
- Linux 系统编程--查看目录文件
- STM32F042开发板学习实践
- firefox中a链接跳转失效问题
- 国外整理的一套在线渗透测试资源合集
- Java不可编写返回引用可变对象的访问器方法
- 记录自已学习之TCP协议
- 二,四,八,十六转化10进制
- SQL连接查询 笔记
- URLConnection类概述与实例