cemal
cemal

Reputation: 1383

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?

Upvotes: 0

Views: 256

Answers (3)

Slrs
Slrs

Reputation: 105

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.

Upvotes: 0

caf
caf

Reputation: 239301

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).

Upvotes: 3

Simone
Simone

Reputation: 11797

'.' 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.

Upvotes: 1

Related Questions