2015-10-05 170 views
0

我正在編寫一個程序,該目錄在目錄中執行時將生成一個包含該目錄中所有內容的文本文件。我得到從**argv主目錄路徑,因爲我使用netbeans和cygwin我必須做一些字符串操縱獲取的路徑在我的char* get_path(char **argv)函數。目錄路徑的大小將總是不一樣,因此我使用malloc來分配空間以將其存儲在內存中。C - 獲取目錄路徑

節目片段:

#include <stdio.h> 
#include <stdlib.h> 
#include <dirent.h> 
#include "dbuffer.h" //my own library 
#include "darray.h" //my own library 

ARR* get_dir_contents(char* path) 
    { 
    DBUFF *buff = NULL; 
    ARR *car = NULL; 
    DIR *dir_stream = NULL; 
    struct dirent *entry = NULL; 

    dir_stream = opendir(path); 
    if(opendir(path)==NULL) printf("NULL"); 

    //... more code here 
    return car; 
    } 

char* get_path(char **argv) 
    { 
    char *path = malloc(sizeof(char)* sizeof_pArray(&argv[0][11]) + 3); 

    strcpy(path, "C:"); 
    strcat(path, &argv[0][11]); 

    printf("%s, sizeof: %d \n",path, sizeof_pArray(path)); 
    return path; 
    } 

int main(int argc, char** argv) 
    { 

    char *p = get_path(argv); 

    ARR *car = get_dir_contents(&p[0]); 

    //... more code here 

    return (EXIT_SUCCESS); 
    } 

的問題是,我有字符串不初始化dir_stream指針。我懷疑這是因爲指針和字符串文字之間存在某些差異,但我無法準確指出它究竟是什麼。另外,dirent庫函數期望的事實可能與它有關。

輸出:

C:/Users/uk676643/Documents/NetBeansProjects/__OpenDirectoryAndListFiles/dist/Debug/Cygwin_4.x-Windows/__opendirectoryandlistfiles, sizeof: 131 
NULL 
RUN FAILED (exit value -1,073,741,819, total time: 2s) 
+3

'sizeof_pArray'聲音不對,爲什麼不使用標準函數strlen的?也不需要將'**'傳遞給get_path,只需將其聲明爲普通的'char *'並傳遞'* argv'就可以使其更簡單。 –

+0

http://stackoverflow.com/questions/298510/how-to-get-the-current-directory-in-ac-program – stark

+0

@Cyber​​Spock它是我自己的函數,獲取字符串長度+終止字符 –

回答

1

這裏有一些事情,可以去錯了,所以 我建議做這樣的事情,而不是

char* get_path(char *argv) 
{ 
    char *path = malloc(sizeof(char)* strlen(argv)); 

    if (path != NULL) 
    { 
    strcpy(path, "C:"); 
    strcat(path, argv + 11); 

    printf("%s, sizeof: %d \n",path, strlen(path)); 
    } 
    return path; 
} 


... 
char* p = get_path(*argv); 

記:你不需要額外的3個字節,因爲你分配包括你稍後跳過的11個字節。儘管不是有11個字節的偏移量,你可能想分解字符串,然後把它放在一起,以便它是可移植的。例如。使用strtok您可以拆分該路徑並替換不需要的零件。

+0

我剛剛試過你的建議,我的問題仍然存在 –

+1

@ShadyProgrammer上面的代碼應該照顧任何指針錯誤,但也許你正在格式化路徑錯誤。調用opendir後檢查errno以確定錯誤。 –

+0

原來,我的指針是正確的,但我得到的路徑包括文件名本身,我必須擺脫路徑的最後一點。我已經使用你建議使用'strtok()'的方法完成了。現在程序正在運行,因爲我想要它。謝謝你的幫助 –

1

難道這是argv的簡單混淆?請在您的main()的開頭插入以下行 ,是您期望的嗎?

printf("\n argv[0]== %s" , argv[0] ); 
getchar(); 

printf("\n argv[1]== %s" , argv[1] ); 
getchar(); 

OK,所以我們從的argv [0]的工作,請嘗試一下本作get_path

char *get_path(char *argv) 
{ 
int i=0; 
// +2 to add the drive letter 
char *path = malloc(sizeof(char)* strlen(argv)+2); 

if (path != NULL) 
{ 
strcpy(path, "C:"); 
strcat(path, argv); 

// we get the path and the name of calling program 
printf("\n path and program== %s",path); 
printf("%s, sizeof: %d \n",path, strlen(path)); 
// now remove calling program name 
    for(i=strlen(path) ; ; i--) 
    { 
    // we are working in windows 
     if(path[i]=='\\') break; 
     path[i]='\0'; 
    } 

} 
return path; 
} 
+0

感謝您的答案,但我敢肯定沒有混淆,加上我驗證檢查: 'argv [0] == /cygdrive/c/Users/uk676643/Documents/NetBeansProjects/__OpenDirectoryAndListFiles/dist/Debug/Cygwin_4.x- Windows/__ opendirectoryandlistfiles argv [1] ==(null)' –

+0

因爲事實證明argv [0]爲我提供了路徑和文件名,我不知道但能夠擺脫文件名。 +1對於'//我們得到調用程序的路徑和名稱。 –