從當前路徑遍歷並搜索文件時,我在查找文件時遇到問題。相對路徑/絕對路徑查詢不適用於當前目錄
我懷疑的是,path
是,由於chdir
怪,在下面的代碼:
相關的文件夾爲:
- 12,13,14,12是父親目錄
- 13的內部12,14的內部13
- david1.txt內部14
- get_current_dir_name()是Linux的功能,而不是我
我跑從Desktop
可執行文件,像這樣的(這裏是printf-s
對於試圖定位問題):
// doesn't work
[email protected]:~/Desktop$ ./exer4 12 david1.txt
Current path is :12
Current path is :/home/a/Desktop/12/13
Current path is :/home/a/Desktop/12/13/14
//////////////////////////////////////////////////
// doesn't work
[email protected]:~/Desktop$ ./exer4 12/13 david1.txt
Current path is :12/13
Current path is :/home/a/Desktop/12/13/14
////////////////////////////////////////////////
// doesn't work
[email protected]:~/Desktop$ ./exer4 12/13/14 david1.txt
Current path is :12/13/14
如果我刪除這些行:
chdir(path); // the problem is probably here
strcpy(path, get_current_dir_name()); // and here
然後每次只涉及單個庫名稱查詢 - 作品,例如:
// works :
[email protected]:~/Desktop$ ./exer4 12 david1.txt
Current path is :12
Current path is :12/13
File found!
12/13/14/david1.txt-rw-rw-r-- 1 1000 1000 0 2012-06-17 08:10
Current path is :12/13/14
////////////////////////////////////////////////////////////
// works
[email protected]:~/Desktop$ ./exer4 12/13 david1.txt
Current path is :12/13
File found!
12/13/14/david1.txt-rw-rw-r-- 1 1000 1000 0 2012-06-17 08:10
Current path is :12/13/14
////////////////////////////////////////////////////////////
// Doesn't work
[email protected]:~/Desktop$ ./exer4 12/13/14 david1.txt
Current path is :12/13/14
任何想法我在做什麼錯在這裏?我試圖改變無數次,但沒有解決它。謝謝
如果您認爲存在問題,爲什麼不檢查返回碼。 – pizza
顯示其餘的代碼,否則很難說。 – rumpel
如何檢查文件是否存在於目錄中?向我們顯示一些代碼以找到錯誤 – MYMNeo