简体   繁体   English

执行期间出现分段错误

[英]getting segmentation fault during execution

I am trying to make a tree structure for a specific directory. 我正在尝试为特定目录制作树形结构。 the path of the directory is entered by the user and is passed to opendir func . 目录的路径由用户输入,并传递给opendir func。 the readdir func reads the current directory and is recursively reading subdirectories. readdir func读取当前目录,并递归读取子目录。 I am unable to dubug this program. 我无法调试该程序。

#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);

}

Every time I execute it ends up with a segmentation fault: Segmentation fault (core dumped) 每次执行时,都会出现分段错误:分段错误(核心已转储)

I am expecting it to cleanly create a dynamic tree with data of each node as name of file or directory. 我期望它可以干净地创建一个动态树,并将每个节点的数据作为文件或目录的名称。

In

_Bool is_dir(const char* path) {
    struct stat buf;
    stat(path, &buf);
    return S_ISDIR(buf.st_mode);
}

stat needs pathname of type char* . stat需要使用char*类型的路径名。

Syntax: 句法:

int stat(const char *pathname, struct stat *statbuf);

but you are passing 但是你过去了

if(is_dir((const char*)dr))

dr of type DIR . DIR类型的dr

Simply change the is_dir calls as below. 只需如下更改is_dir调用即可。

is_dir(dpointer->d_name) //in main as well as in getinto function

Also readdir will return the entries including 同样, readdir将返回以下条目:

.   //current dir
..  //parent dir

Hence you need to skip these two entries in main otherwise your opendir in getinfo will fail causing readdir in getinfo crash. 因此,您需要跳过main这两个条目,否则getinfoopendir将失败,从而导致getinfo崩溃时的readdir

So skip these entries in main as below. 因此,如下跳过main这些条目。

while ((dpointer=readdir(dr))!=NULL){
    if (strcmp(dpointer->d_name,"..")==0 || strcmp(dpointer->d_name,".")==0 ) 
    continue;
   .....
   .....
  }

this code finally worked perfectly for me ' 这段代码最终对我来说非常完美'

#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 ;

}

' thanks for the help y'all !!! 谢谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM