简体   繁体   中英

listing directory files problem

When I list the files by the code below:

/*
 * 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);
}

the files are:

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~

What are number 5. and 8. if shoe that a file with name '.' or '..'. why it is occured. What is the problem?

Every directory on a POSIX filesystem contains an entry . which is a link to itself, and .. which is a link to its parent directory. (The root directory is its own parent).

'.' and '..' are two directories that are always present, it's not an error.

In fact, if on the bash you write

cd .

or

cd ..

it works fine.

In every directory listing you are to do,

"." will always appear first, it's the current directory, the directory you were in once you did an "opendir()" of "."

".." will always appear second, it is the previous directory, the one you acceded your current directory from,

underneath "..", all the rest that appears, will be the directories or files you did create or that already were present inside the current directory ".", in that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM