получение ошибки сегментации во время выполнения - PullRequest
0 голосов
/ 20 октября 2018

Я пытаюсь создать древовидную структуру для определенного каталога.путь к каталогу вводится пользователем и передается в 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);

}

Каждый раз, когда я выполняю ее, она завершается ошибкой сегментации: Ошибка сегментации (ядро сброшено)

Я ожидаю, что он чисто создаст динамическийдерево с данными каждого узла в качестве имени файла или каталога.

Ответы [ 2 ]

0 голосов
/ 20 октября 2018

этот код наконец-то отлично сработал для меня '

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

}

' спасибо за помощь, вы !!!

0 голосов
/ 20 октября 2018

В

_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))

dr типа DIR.

Просто измените is_dir вызовы, как показано ниже.

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

Также readdir вернет записи, включающие

.   //current dir
..  //parent dir

Следовательно, вам нужно пропустить эти две записи в main, иначе ваш opendir в getinfo не удастся вызвать readdir в getinfo сбое.

Поэтому пропустите эти записи в main, как показано ниже.

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