linux创建文件树,孩子兄弟树(或广义表),创建文件树及其訪问
假设在Linux下要訪问一个目录。
我们须要知道一下系统调用.
1.opendir(path); //注意path是绝对路径
2.ptr=readdir(dir);//dir 为opendir();正常打开的目录 DIR dir.假设弱国訪问dir下的全部目录后。readdir()返回NULL,struct dirent ptr; 记录了文件的基本内容
3.stat(file,&buf)//struct stat buf;//将file(文件的绝对路径)。的状态放入buf中
以下以一个小列子来说明,若和显示目录下文件的信息
- #include <stdio.h>
- #include <string.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- int main(){
- DIR *dir;
- struct dirent *ptr;
- char path[20]="/";
- struct stat buf;
- if((dir=opendir(path))==NULL){//打开目录
- perror("error:");
- }else{
- while((ptr=readdir(dir))!=NULL){//读取目录下的内容
- char file[30]="/";
- strcpy(file+strlen(file),ptr->d_name);
- lstat(file,&buf);//获取文件的绝对路径
- printf("%s ",ptr->d_name);//输出文件名称
- if(S_ISDIR(buf.st_mode)){//假设文件是目录,输出DIR
- printf(" DIR\n");
- }else{
- printf(" \n");
- }
- }
- closedir(dir);//关闭目录子
- }
- return 0;
- }
执行结果
显示了根文件夹下的情况.
2.若果要实现ls -R 的功能.
1.能够非常方便的递归訪问(可是系统栈有限有溢出可能)
2.我们用构造文件,文件夹树装结构.
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvU2FwcGhpcmVTdGFydA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
a.我们能够以队列层次展开顺寻为.1,2,3,4,.....13;(对于整个文件系统若果用队列来存储的话,可能所需空间。不能得到满足,我们的程序可能无法获得这麽大的空间),而且顺序也不合理
b.我们建立局部(这里以完整的树为例,加上里面的free变为局部树),来实现所有先序的訪问.
1.分析我们对于一个文件夹,打开能够得到他的全部子文件夹,一次仅仅要我们获得一个根节点的位置,就可建立它与它的直接孩子的联系,在此时訪问这些孩子.
没有孩子
2.若没有不论什么孩子,且有右兄弟。那麽訪问有兄的的树.訪问它
3.没有孩子,没有有兄弟,那麽该节点的父亲节点为根的树已经所有訪问完成.此时若父亲节点有右孩子,那麽訪问它.
4.除2,3,且有父亲节点。那麽当前节点父亲节点作为当前节点,继续寻找知道出现1,2,就能够訪问,
有孩子
訪问首个孩子.
就这样能够用先序列訪问该树.
×在带吗运行中弱国端错说明堆当期那文件訪问权限不够×
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <string.h>
- char path[5000];//文件夹长度
- int plength=0;
- long c=0;
- char temppath[5000];
- typedef struct node {
- char name[82];//记录位置的信息path[plength]+current->name来获取完整路径信息
- struct node* child;
- struct node* brother;
- struct node* father;
- }*cbnode;
- cbnode getCbnode(){ //用于获得新的节点
- cbnode cb=(cbnode)malloc(sizeof(struct node));
- cb->child=cb->brother=cb->father=NULL;
- return cb;
- }
- cbnode list(const char * rootpath){//传递调用位置的操作,所要展开的文件树根节点
- DIR *dir=NULL;
- struct dirent *ptr=NULL;//文件夹的列表项
- struct stat buf;//获取文件加文件夹
- strcpy(path,rootpath);
- plength=strlen(path);//获取路径长度
- strcpy(temppath,path);//拷贝到当前路径情况
- cbnode root=NULL,current=NULL;
- root=getCbnode();//获取当前节点
- strcpy(root->name,rootpath);
- current=root;
- if((dir=opendir(current->name))==NULL){//初始给定非文件夹文件夹
- printf("no such file or direct! %s\n",current->name);
- return NULL;
- }
- while(10){
- int count=0;//当前訪问到第几个节点
- cbnode previous=NULL;//除了第一个节点,其它节点都有上个节点
- while((ptr=readdir(dir))!=NULL){//产生树遍历子文件夹输出
- strcpy(temppath+plength,ptr->d_name);
- temppath[plength+strlen(ptr->d_name)]='\0';//得到详细文件位置
- if(strcmp(ptr->d_name,".")&&strcmp(ptr->d_name,"..")){
- printf("%s\n",temppath);//打印文件名
- }
- if(lstat(temppath,&buf)==-1){
- printf("stat error\n");
- }
- if(S_ISDIR(buf.st_mode)&&strcmp(ptr->d_name,".")&&strcmp(ptr->d_name,"..")){
- cbnode cb=getCbnode();
- //printf("dirpath:%s\n",temppath);
- //printf("%s \n",temppath);
- strcpy(cb->name,ptr->d_name);
- strcat(cb->name,"/");
- cb->name[strlen(cb->name)]='\0';
- if(count==0){
- previous=cb;
- current->child=cb;
- cb->father=current;
- }else{ //创建孩子链
- previous->brother=cb;
- cb->father=previous->father;
- previous=cb;
- }
- count++;
- //printf("dir:%ld\n",c);
- }
- }
- closedir(dir);
- if(count==0){//假设该节点下没有子文件夹
- while(10){
- if(current->brother){
- plength-=strlen(current->name);
- //free(current);
- current=current->brother;
- strcpy(path+plength,current->name);
- plength+=strlen(current->name);
- path[plength]='\0';
- strcpy(temppath,path);
- break;
- }else if(current->father&¤t->father->brother){
- plength-=(strlen(current->name)+strlen(current->father->name));
- //free(current);
- current=current->father->brother;
- strcpy(path+plength,current->name);
- plength+=strlen(current->name);
- path[plength]='\0';
- strcpy(temppath,path);
- break;
- }else if(current->father&¤t->father->brother==NULL){
- plength-=strlen(current->name);
- path[plength]='\0';
- // free(current);
- current=current->father;//寻找上个节点
- strcpy(temppath,path);
- }else if(current->father==NULL){
- return NULL;//訪问结束
- }else{
- printf("default\n");
- }
- }
- dir=opendir(path);
- }else{
- current=current->child;
- strcpy(path+plength,current->name);//当前节点为最后一个节
- plength+=strlen(current->name);
- path[plength]='\0';
- strcpy(temppath,path);
- dir=opendir(path);
- }
- }
- }
- int main(int argc,char *argv[]){
- printf("%s\n",argv[1]);
- list(argv[1]);
- return 0;
- }
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <string.h>
- char path[5000];//文件夹长度
- int plength=0;
- long c=0;
- char temppath[5000];
- typedef struct node {
- char name[82];//记录位置的信息path[plength]+current->name来获取完整路径信息
- struct node* child;
- struct node* brother;
- struct node* father;
- }*cbnode;
- cbnode getCbnode(){ //用于获得新的节点
- cbnode cb=(cbnode)malloc(sizeof(struct node));
- cb->child=cb->brother=cb->father=NULL;
- return cb;
- }
- cbnode list(const char * rootpath){//传递调用位置的操作,所要展开的文件树根节点
- DIR *dir=NULL;
- struct dirent *ptr=NULL;//文件夹的列表项
- struct stat buf;//获取文件加文件夹
- strcpy(path,rootpath);
- plength=strlen(path);//获取路径长度
- strcpy(temppath,path);//拷贝到当前路径情况
- cbnode root=NULL,current=NULL;
- root=getCbnode();//获取当前节点
- strcpy(root->name,rootpath);
- current=root;
- if((dir=opendir(current->name))==NULL){//初始给定非文件夹文件夹
- printf("no such file or direct! %s\n",current->name);
- return NULL;
- }
- while(10){
- int count=0;//当前訪问到第几个节点
- cbnode previous=NULL;//除了第一个节点。其它节点都有上个节点
- while((ptr=readdir(dir))!=NULL){//产生树遍历子文件夹输出
- strcpy(temppath+plength,ptr->d_name);
- temppath[plength+strlen(ptr->d_name)]='\0';//得到详细文件位置
- if(strcmp(ptr->d_name,".")&&strcmp(ptr->d_name,"..")){
- printf("%s\n",temppath);//打印文件名
- }
- if(lstat(temppath,&buf)==-1){
- printf("stat error\n");
- }
- if(S_ISDIR(buf.st_mode)&&strcmp(ptr->d_name,".")&&strcmp(ptr->d_name,"..")){
- cbnode cb=getCbnode();
- //printf("dirpath:%s\n",temppath);
- //printf("%s \n",temppath);
- strcpy(cb->name,ptr->d_name);
- strcat(cb->name,"/");
- cb->name[strlen(cb->name)]='\0';
- if(count==0){
- previous=cb;
- current->child=cb;
- cb->father=current;
- }else{ //创建孩子链
- previous->brother=cb;
- cb->father=previous->father;
- previous=cb;
- }
- count++;
- //printf("dir:%ld\n",c);
- }
- }
- closedir(dir);
- if(count==0){//假设该节点下没有子文件夹
- while(10){
- if(current->brother){
- plength-=strlen(current->name);
- //free(current);
- current=current->brother;
- strcpy(path+plength,current->name);
- plength+=strlen(current->name);
- path[plength]='\0';
- strcpy(temppath,path);
- break;
- }else if(current->father&¤t->father->brother){
- plength-=(strlen(current->name)+strlen(current->father->name));
- //free(current);
- current=current->father->brother;
- strcpy(path+plength,current->name);
- plength+=strlen(current->name);
- path[plength]='\0';
- strcpy(temppath,path);
- break;
- }else if(current->father&¤t->father->brother==NULL){
- plength-=strlen(current->name);
- path[plength]='\0';
- // free(current);
- current=current->father;//寻找上个节点
- strcpy(temppath,path);
- }else if(current->father==NULL){
- return NULL;//訪问结束
- }else{
- printf("default\n");
- }
- }
- dir=opendir(path);
- }else{
- current=current->child;
- strcpy(path+plength,current->name);//当前节点为最后一个节
- plength+=strlen(current->name);
- path[plength]='\0';
- strcpy(temppath,path);
- dir=opendir(path);
- }
- }
- }
- int main(int argc,char *argv[]){
- printf("%s\n",argv[1]);
- list(argv[1]);
- return 0;
- }
linux创建文件树,孩子兄弟树(或广义表),创建文件树及其訪问的更多相关文章
- UVa11732 "strcmp()" Anyone?(Trie树+孩子兄弟表示法)
我的做法是先建字典树,统计每个结点出现次数和相同字符串个数,每个结点对答案的贡献就是2*C(次数,2),然后再分别讨论相同字符串和不同字符串对答案的贡献. 另外这题主要就是Trie树的孩子兄弟表示法: ...
- 如何使用PHP上传文件,上传图片,php上传教程,php表单文件上传教程
使用PHP进行文件上传,主要使用到表单功能和PHP内置的$_FILES函数功能.接下来我们看如何实现PHP上传功能.例子效果图,此例子是在Mac下进行调试成功的. PHP上传图片文件的功能代码如下: ...
- [加入用户]解决useradd 用户后没有加入用户Home文件夹的情况,Linux改变文件或文件夹的訪问权限命令,linux改动用户password,usermod的ysuum安装包。飞
usermod的yum安装包: shadow-utils 将nobody用户加入到nogroup 组: usermod -g nogroup nobody cat /etc/passwd|grep n ...
- 为什么 MySQL 索引要使用 B+树而不是其它树形结构?比如 B 树?
一个问题? InnoDB一棵B+树可以存放多少行数据?这个问题的简单回答是:约2千万 为什么是这么多呢? 因为这是可以算出来的,要搞清楚这个问题,我们先从InnoDB索引数据结构.数据组织方式说起. ...
- SOLR对多个(关联)表创建索引
又两天没写博客,关于SOLR的东西,写了一周了还没写完我也是醉了,毕竟会的东西真不多,周四晚上加班没写,周五晚上公司同事聚会也没写,今天在家,还是把最后的一点写完吧,我会的剩下的也就是一个对多个表创建 ...
- apache禁止訪问某些文件或文件夹的方法
[apache配置禁止訪问] 1. 禁止訪问某些文件/文件夹 添加Files选项来控制,比方要不同意訪问 .inc 扩展名的文件,保护php类库: <Files ~ "\.inc$&q ...
- 选课 - 树型DP(孩子兄弟建树法)
题目描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了 N(N<300)门的选修课程,每个学生可选课程的数量 M 是给定的.学生选修了这M门课并考核通 ...
- Linux下创建与解压tar, tar.gz和tar.bz2文件及压缩率对比 | 沉思小屋
刚 在qq群里面一位仁兄问到文件压缩的命令,平时工作中大多用解压缩命令,要是遇到压缩就现查(这不是一个好习惯),于是整理下Linux下创建与解压 zip.tar.tar.gz和tar.bz2文件及他们 ...
- Linux编程 5 (目录重命名与移动mv,删除文件rm,目录创建mkdir删除rmdir,查看file,cat,more,tail,head)
一. 文件重命名与移动(mv) 在linux中,重命名文件称为移动(moving).mv命令可以将文件和目录移动到另一个位置或重新命名. 1.1 使用mv重命名 下面在/usr/local下面创建一个 ...
随机推荐
- SQL Server 从数据库快照还原数据库
语法: restore database db_name from database_snapshot = 'db_snapshot_name'; ------------------------- ...
- android apk 导出(签名) is not translated in xx 代码混淆 反编译
apk导出遇到问题 解决方式如下 1.导出步骤第一步 2.提示错误 3.解决 其余步骤参见: 代码混淆和数字签名(现在版本混淆) http://blog.csdn.net/moruna/article ...
- node配置运行环境变量;
node express 在开发环境和生产环境运行的代码是不一样的, 通常是先配置好的,在开发环境运行一套代码,在生产环境运行另一套代码, 开发环境 development, 生产环境producti ...
- HDU Today(dijskra)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- SQL事务与并发
1.Transaction(事务)是什么: 事务是作为单一工作单元而执行的一系列操作.包括增删查改. 2.事务的种类: 事务分为显示事务和隐式事务: 隐式事务:就是平常我们使用每一条sql 语句就是一 ...
- jQuery源码笔记——四
each()实现 var jQuery = function( selector, context ) { return new jQuery.fn.init( selector, context ) ...
- Android Handler Leak
转自:Android中使用Handler引发的内存泄露 在Activity中,经常会用到自定义的Handler来处理主线程收到的Message,但是ADT20以后,直接定义的如下定义的内部会有提示说这 ...
- SVN导出增量包的方法
此方法是在svn1.7版本基础上进行的操作,其他版本没有验证 第一步.点击右键,选择“TortoiseSVN–> Show log”. 进入日志页面,如下图所示: 第二步.选择版本区间,右键选择 ...
- Extjs springmvc session 超时 处理
如果你的项目使用ExtJS作为表现层,你会发现,SESSION超时控制将是一个问题.本文将就自己的经验,来解决这一问题,当然,解决问题并非只有一种方法,我只是提出我的方法.首先,做超时控制,必需使用过 ...
- js监听input等表单输入框的变化事件oninput
js监听input等表单输入框的变化事件oninput,手机页面开发中使用到文本框textarea输入字符监听文本框变化计算还可以输入多少字符,如果使用onkeyup的话是无法监听到输入法输入的文本变 ...