2011-09-27 74 views
-4

嗨,大家好,我正在嘗試解析currnet工作目錄並將反斜槓更改爲正斜槓。但我得到的輸出喜歡這個C解析問題

Current Working Directory = /cygdrive/c/cworkspace/helloWorld 
33 
Current Working Directory = \cygdrive\c\cworkspace\helloWorld 

代碼:

#include <sys/types.h> 
#include <sys/dir.h> 
#include <sys/param.h> 
#include <stdio.h> 
char pathname[MAXPATHLEN]; 
main() 
{ 
    int i =0; 
    (char *) getwd(pathname); 
    printf("Current Working Directory = %s\n %d\n",pathname, strlen(pathname)); 
    for (i = 0; i < strlen(pathname); i++) { 
     if (pathname[i] == '/'){ 
     pathname[i] = '\\'; 
     } 
    } 
    printf("Current Working Directory = %s\n",pathname); 
} 
+1

你期待看到什麼? – NPE

+1

刪除for循環:) –

+1

輸出完全按照你所說的應該:/ –

回答

2

如果你想從反斜線改爲正斜線,應該不是你的條件是:

if (pathname[i] == '\\'){ 
    pathname[i] = '/'; 
} 
1

我不清楚輸出是錯誤的。它確實會改變你描述的斜槓,這是你想要的,對吧?

如果您在輸出中詢問額外的「33」,那是因爲您在第一個printf語句中打印出當前工作目錄的長度。

---編輯---

好吧,我看到其他人知道一個正斜槓和反斜槓之間的差異。正如Luchian在下面描述的那樣,您正在比較正斜線並替換反斜槓,這與您打算做的事情相反。看他的帖子糾正。

+0

這與他所描述的相反,他的病情是錯誤的。 –

+0

更正,更改投票。 –