[英]listing directory files problem
當我通過以下代碼列出文件時:
/*
* This program displays the names of all files in the current directory.
*/
#include <dirent.h>
#include <stdio.h>
int main(void)
{
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
printf("%s\n", dir->d_name);
}
closedir(d);
}
return(0);
}
這些文件是:
1. client.c
2. deneme.c
3. server.c
4. chat.h~
5. .
6. makefile~
7. udpClient.c~
8. ..
9. udpServer.cpp~
10. client
11. chat.h
12. udpServer.c~
13. server
14. makefile
15. deneme.c~
什么是數字5和數字8.如果插入名稱為“。”的文件 要么 '..'。 為什么會發生。 問題是什么?
POSIX文件系統上的每個目錄都包含一個條目.
這是指向自身的鏈接,而..
是指向其父目錄的鏈接。 (根目錄是其自己的父目錄)。
'' 和“ ..”是兩個始終存在的目錄,這不是錯誤。
實際上,如果你大膽地寫
cd .
要么
cd ..
它工作正常。
在您要做的每個目錄中,
“” 始終會首先顯示,它是當前目錄,即您對“”執行“ opendir()”后所在的目錄。
“ ..”將始終顯示在第二個目錄中,它是前一個目錄,即您從中加入當前目錄的目錄,
在這種情況下,“ ..”下面顯示的所有其余部分將是您確實創建的目錄或文件,或者已經存在於當前目錄“。”中的目錄或文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.