Why do directory listings contain the current (.) and parent (..) directory?

后端 未结 8 1169
面向向阳花
面向向阳花 2020-12-11 03:11

Whenever I list the contents of a directory with a function like readdir, the returned file names also include \".\" and \"..\". I have the suspicion that these are just nor

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 03:29

    . and .. are actually hard links in filesystems. They are needed so that you can specify relative paths, based on some reference path (consider "../sibling/file.txt"). Since these hard links are actually existing in the filesystem, it makes sense for readdir to tell you about them. (actually the term hard link just means some name that is indistinguishable from the actual directory referred to: they both point to the same inode in the filesystem).

    Best way is to just strcmp and ignore them, if you don't want to list them.

提交回复
热议问题