2017-05-01 3 views
-2

아래 함수를 사용하여 재귀 적으로 디렉토리를 추출하려고합니다. 프로그램을 실행하면 루트 디렉토리와 그 안에있는 파일을 만들 수 있지만 하위 디렉토리 안에 파일을 완전히 쓸 수는 없습니다.디렉토리를 재귀 적으로 추출하는 방법은 무엇입니까?

내 코드를 컴파일 할 수는 있지만 세그먼트 오류 오류가 계속 발생합니다. 내 함수 나 메모리에서 다른 포인터로 접근해서는 안되지만 아무렇지도 않게되는 악의적 인 포인터를 찾으려고 노력했다. 올바른 방향으로 도움이나 안내를 해주시면 매우 감사하겠습니다! 여기

내 추출 기능입니다 : 눈에서

int DExtract(FILE* fp, char* outputName, char* inputName, DIR* d, bool root) 
{ 
     DIR* dsub; 
    struct dirent* p; 
    FILE* outF; 
    int i=0; 
    char fileName[BUFFER_SIZE]; 
    char SfileName[BUFFER_SIZE]; 
    bool SC; 
    int dz; 
    int nameLength; 
    int fileSize; 
    int fileType; 

////// ROOT CREATION ///////////// 
if(root) 
{ 
     fscanf(fp,"%d %d %s %d\n", &fileType, &nameLength, inputName, &fileSize); 
    if(fileType==0) 
    { 
    mkdir(outputName, 0755); // create root directory 
    } 

} 
////////////////////////////////// 

root = false; // MOVING INTO ROOT 
d = opendir(outputName); // open directory with input name 

while((p = readdir(d)) || (dz != EOF)) 
    { fscanf(fp,"%d %d %s %s %d ",&fileType, &nameLength, p->d_name, fileName, &fileSize); 

////////////////////////////////////////////////// 

    strcpy(SfileName,outputName); 

    for(int h=0; h < strlen(fileName); h++) 
    { 
     if(fileName[h] == '/') 
     { SC = true;} 

     if(SC == true) 
     { strcat(SfileName, &fileName[h]); 

      h=strlen(fileName); 
     } 
    } 
     SC = false; 
    cout << "STRING LENGTH FILENAME: " << strlen(fileName) << endl; 
    cout << "SFILENAME in WHILE: " << SfileName << endl; 






///////////////////////////////////// ///////////////// 

    if(strcmp(p->d_name, ".") != 0) // if they are not the same as current working directory 
    { if(strcmp(p->d_name, "..") != 0) // if they are not the same as parent directory 

     { if(p->d_name[nameLength - 1] != '~') // If not the same as home directory 
      { 

/////////////////////////// 
    if(fileType == 1) 
    { 

    // This is a regular file 

    outF = fopen(SfileName, "w+"); // create file 
    i=0; 


    do 
    { 
     int ch; 
     ch = fgetc(fp); 
     dz = ch; 
     if(i != (fileSize-1)) 
     { 
      fputc(ch,outF); 
      i++; 

     } 

    }while(i != (fileSize-1)); 
    fclose(outF); 

} 
else if(fileType == 0) 
{ 

    // This is a directory 

cout << "SFILENAME in DIRECTORY: " << SfileName << endl; 

    mkdir(SfileName, 0755); // create directory 

    if((strcmp(SfileName, ".") != 0)) 
    { 
     dsub = opendir(SfileName); 
     DExtract(fp, outputName, inputName, dsub, root); 

     } 
    } 
    } 
    } 
    } 
} 


    return 0; 
} 
+0

'fscanf()'를 사용하여'readdir()'로 방금 읽은'p-> d_name'을 덮어 쓰는 이유는 무엇입니까? – Barmar

+0

'if (p-> d_name [nameLength - 1]! = '~')'이것은 작동하지 않습니다. 홈 디렉토리는 실제로'~'로 이름 지어지지 않으며 쉘과 다른 프로그램에서 사용되는 규칙 일뿐입니다. 그들은'$ HOME' 환경 변수의 값으로 대체합니다. – Barmar

+0

"디렉토리 추출"의 의미는 무엇입니까? 재귀 적으로 파일을 나열하거나 파일을 재귀 적으로 작성하는 데 문제가 있습니까? –

답변

0

,이 가능한 범인 :

strcpy(SfileName,outputName); 
당신은 소스 문자열을 초과 할 경우 대상 문자열에 복사하려고 할 수있다

버퍼 크기, 정의되지 않은 동작이 발생합니다.

관련 문제