c - 在执行期间出现分段错误
问题描述
我正在尝试为特定目录创建树结构。目录的路径由用户输入并传递给 opendir func 。readdir 函数读取当前目录并递归读取子目录。我无法调试这个程序。
#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("helllo\n");
rootnode->i=0;
dr=opendir(path);
//printf("helllo\n");
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("helllo\n");
if(is_dir(dpointer->d_name)){
printf("%s is directory \n",dpointer->d_name);
getinto(dpointer->d_name,path,rootchild);
//printf("helllo loop\n");
printf("%s is directory \n",dpointer->d_name);
}
else{
strcpy(rootchild->dname,dpointer->d_name);
//printf("helllo loop\n");
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 function\n");
d=opendir(currentpath);
//printf("helllo function\n");
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 loop\n");
if(is_dir(dpointer->d_name)){
printf("%s is directory \n",dpointer->d_name);
getinto(dpointer->d_name,currentpath,child);
//printf("helllo loop\n");
printf("%s is directory \n",dpointer->d_name);
}
else{
strcpy(child->dname,dpointer->d_name);
//printf("helllo loop\n");
printf("%s is file \n",dpointer->d_name);
}
}
closedir(d);
}
每次执行时都会出现分段错误:分段错误(核心转储)
我期望它干净地创建一个动态树,其中每个节点的数据作为文件或目录的名称。
解决方案
这段代码终于完美地为我工作了'
#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("helllo\n");
rootnode->i=0;
dr=opendir(path);
//printf("helllo\n");
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("helllo\n");
if(is_dir(dpointer->d_name)){
printf("%s is directory \n",dpointer->d_name);
getinto(dpointer->d_name,path,rootchild);
//printf("helllo loop\n");
//printf("%s is directory \n",dpointer->d_name);
}
else{
strcpy(rootchild->dname,dpointer->d_name);
//printf("helllo loop\n");
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 function\n");
d=opendir(currentpath);
//printf("helllo function\n");
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 loop\n");
if(is_dir(dpointer->d_name)){
printf("%s is directory \n",dpointer->d_name);
//getinto(dpointer->d_name,currentpath,child);
//printf("helllo loop\n");
printf("%s is directory \n",dpointer->d_name);
}
else{
strcpy(child->dname,dpointer->d_name);
//printf("helllo loop\n");
printf("%s is file \n",dpointer->d_name);
}
}
printf("\niits over!!");
closedir(d);
return ;
}
'谢谢大家的帮助!!!
推荐阅读
- python - Python - 从变量中提取数据的正则表达式
- r - 如何将 stl 输出转换为数据帧但保持时间序列时间步长?
- node.js - Sqlite3 安装卡在“unpacking node_sqlite3.node”
- javascript - Webpack HtmlWebpackPlugin 移除 DOM 元素
- php - PHP password_verify 不能与准备好的语句一起使用
- asp.net - Sql Exception 向现有查询添加临时列时语法不正确
- r - R download.file 带有“wget”-方法并指定额外的 wget 选项
- c# - 如何使用 Json (C#) 序列化忽略某些属性的对象
- r - 在 R 中翻译数据框中的行
- javascript - 将 JS lib/plugins 和 CSS 文件打包到一个文件中是否正确?