概述
如果在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(" DIRn");
}else{
printf(" n");
}
}
closedir(dir);//关闭文件夹子
}
return 0;
}
运行结果
![](https://file2.kaopuke.com:8081/files_image/2023060722/202306072205114104811.png)
2.若果要实现ls -R 的功能.
1.可以很方便的递归访问(但是系统栈有限有溢出可能)
2.我们用构造文件,目录树装结构.
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! %sn",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)]='