我正在尝试为特定目录创建一个树结构。 目录的路径由用户输入并传递给 OpenDir func 。 readdir func 读取当前目录并以递归方式读取子目录。我无法欺骗这个程序。
#include<stdio.h>
#include<dirent.h>
#include<string.h>
#include<malloc.h>
#include<sys/stat.h>
#include<limits.h>
#include<stdlib.h>
#define _GNU_SOURCE
struct tree{
char dname[100];
char fname[200];
int i;
struct tree *openf[100];
};
void getinto(char [],char [],struct tree*);
struct dirent *dpointer;
int found=0;
int k=0;
_Bool is_dir(const char* path) {
struct stat buf;
stat(path, &buf);
return S_ISDIR(buf.st_mode);
}
int main() {
char path[100]=".";
DIR *dr;
struct tree *rootnode;
rootnode=(struct tree*)malloc(sizeof(struct tree));
printf("enter path :: ");
scanf("%s",path);
//printf("helllon");
rootnode->i=0;
dr=opendir(path);
//printf("helllon");
strcpy(rootnode->dname,path);
if((dpointer=readdir(dr))==NULL){
printf("current directory is empty !!");
}
while ((dpointer=readdir(dr))!=NULL){
struct tree *rootchild;
rootchild=(struct tree*)malloc(sizeof(struct tree));
rootnode->openf[rootnode->i++]=rootchild;
if (strcmp(dpointer->d_name,"..")==0 || strcmp(dpointer->d_name,".")==0 )
continue;
//printf("helllon");
if(is_dir(dpointer->d_name)){
printf("%s is directory n",dpointer->d_name);
getinto(dpointer->d_name,path,rootchild);
//printf("helllo loopn");
printf("%s is directory n",dpointer->d_name);
}
else{
strcpy(rootchild->dname,dpointer->d_name);
//printf("helllo loopn");
printf("%s is file n",dpointer->d_name);
}
}
closedir(dr);
return 0;
}
void getinto(char sfilename[],char spath[],struct tree* this){
char filename[100],currentpath[100],temp[100];
DIR *d=opendir(currentpath);
strcpy(filename,sfilename);
strcpy(currentpath,spath);
strcat(currentpath,"/");
strcat(currentpath,filename);
printf("helllo functionn");
d=opendir(currentpath);
//printf("helllo functionn");
this->i=0;
while((dpointer=readdir(d))!=NULL){
struct tree *child;
child=(struct tree*)malloc(sizeof(struct tree));
this->openf[this->i++]=child;
if (strcmp(dpointer->d_name,"..")==0 || strcmp(dpointer->d_name,".")==0 )
continue;
//printf("helllo function loopn");
if(is_dir(dpointer->d_name)){
printf("%s is directory n",dpointer->d_name);
getinto(dpointer->d_name,currentpath,child);
//printf("helllo loopn");
printf("%s is directory n",dpointer->d_name);
}
else{
strcpy(child->dname,dpointer->d_name);
//printf("helllo loopn");
printf("%s is file n",dpointer->d_name);
}
}
closedir(d);
}
每次我执行它时,都会出现分段错误: 分段故障(核心转储(
我希望它能够干净地创建一个动态树,每个节点的数据作为文件或目录的名称。
in
_Bool is_dir(const char* path) {
struct stat buf;
stat(path, &buf);
return S_ISDIR(buf.st_mode);
}
stat
需要类型为char*
的路径名。
语法:
int stat(const char *pathname, struct stat *statbuf);
但你正在通过
if(is_dir((const char*)dr))
DIR
型dr
。
只需更改is_dir
调用,如下所示。
is_dir(dpointer->d_name) //in main as well as in getinto function
此外,readdir
将返回包括
. //current dir
.. //parent dir
因此,您需要跳过这两个条目main
否则您在getinfo
中的opendir
将失败,导致readdir
getinfo
崩溃。
因此,请按如下方式跳过main
这些条目。
while ((dpointer=readdir(dr))!=NULL){
if (strcmp(dpointer->d_name,"..")==0 || strcmp(dpointer->d_name,".")==0 )
continue;
.....
.....
}
这段代码终于对我完美地工作了'
#include<stdio.h>
#include<dirent.h>
#include<string.h>
#include<malloc.h>
#include<sys/stat.h>
#include<limits.h>
#include<stdlib.h>
#define _GNU_SOURCE
struct tree{
char dname[1000];
char fname[2000];
int i;
struct tree *openf[1000];
};
void getinto(char [],char [],struct tree*);
struct dirent *dpointer;
int found=0;
int k=0;
_Bool is_dir(const char* path) {
struct stat buf;
stat(path, &buf);
return S_ISDIR(buf.st_mode);
}
int main() {
char path[1000]=".";
DIR *dr;
struct tree *rootnode;
rootnode=(struct tree*)malloc(sizeof(struct tree));
printf("enter path :: ");
scanf("%s",path);
//printf("helllon");
rootnode->i=0;
dr=opendir(path);
//printf("helllon");
strcpy(rootnode->dname,path);
if((dpointer=readdir(dr))==NULL){
printf("current directory is empty !!");
}
while ((dpointer=readdir(dr))!=NULL){
struct tree *rootchild;
rootchild=(struct tree*)malloc(sizeof(struct tree));
rootnode->openf[rootnode->i++]=rootchild;
if (strcmp(dpointer->d_name,"..")==0 || strcmp(dpointer->d_name,".")==0 )
continue;
//printf("helllon");
if(is_dir(dpointer->d_name)){
printf("%s is directory n",dpointer->d_name);
getinto(dpointer->d_name,path,rootchild);
//printf("helllo loopn");
//printf("%s is directory n",dpointer->d_name);
}
else{
strcpy(rootchild->dname,dpointer->d_name);
//printf("helllo loopn");
printf("%s is file n",dpointer->d_name);
}
}
closedir(dr);
return 0;
}
void getinto(char sfilename[],char spath[],struct tree* this){
char filename[1000],currentpath[1000],temp[1000];
DIR *d=opendir(currentpath);
strcpy(filename,sfilename);
strcpy(currentpath,spath);
strcat(currentpath,"/");
strcat(currentpath,filename);
printf("%s",currentpath);
printf("nhelllo functionn");
d=opendir(currentpath);
//printf("helllo functionn");
this->i=0;
while((dpointer=readdir(d))!=NULL){
struct tree *child;
child=(struct tree*)malloc(sizeof(struct tree));
this->openf[this->i++]=child;
if (strcmp(dpointer->d_name,"..")==0 || strcmp(dpointer->d_name,".")==0 )
continue;
//printf("helllo function loopn");
if(is_dir(dpointer->d_name)){
printf("%s is directory n",dpointer->d_name);
//getinto(dpointer->d_name,currentpath,child);
//printf("helllo loopn");
printf("%s is directory n",dpointer->d_name);
}
else{
strcpy(child->dname,dpointer->d_name);
//printf("helllo loopn");
printf("%s is file n",dpointer->d_name);
}
}
printf("niits over!!");
closedir(d);
return ;
}
' 感谢您的帮助!!