這可能只是一個語法上的事情,我做錯了,但我不能爲我的生活弄清楚,所以我很抱歉,如果這是太「HEY爲我調試我的代碼!」C目錄遍歷 - 它不應該打印目錄名稱
相關代碼:
struct dirent *readDir;
DIR *dir;
dir = opendir(name);
if(dir == NULL) {
printf("No directory found with the name %s\n", name);
} else {
printf("directory named %s opened.\n", name);
while((readDir = readdir(dir)) != NULL) {
if(readDir->d_name != ".." || readDir->d_name != ".") {
printf("%s\n", readDir->d_name);
}
}
closedir(dir);
}
的如果while循環條件似乎並不工作,這裏的輸出它產生:
directory named test opened.
file2
.
test2
file1
..
如果我沒有弄錯,如果聲明應該過濾掉。和..目錄,但它不。這樣做的目標是進行遞歸目錄遍歷,但是除非我能夠避免它重複進入。和..我不能真正移動的目錄。
基本上,我不知道如何做字符串比較我猜?
謝謝。如果不是很明顯,我是C新手。感謝幫助。 – mrowland