A readdir() issue
My intention is to read every directory and file rooted at a directory given as input in a depth-first manner and towards it I had written a portion(very very initial) of code as shown below.
int main()
{
DIR *fd_dir;
struct di开发者_如何学Gorent *s_dirent;
struct stat buff;
char str[100];
fd_dir = opendir("/home/juggler");
if(fd_dir == 0)
printf("Error opening directory");
while((s_dirent = readdir(fd_dir)) != NULL)
{
printf("\n Name %s",s_dirent->d_name);
}
closedir(fd_dir);
}
Now, the directory juggler has 3 directories say A, B and C but an output to this program not only gives these three directories but also .mozilla .zshrc .gvfs .local .bash_history etc which i do not see when opening juggler normally.
What are these extra things inside juggler and how do I not read them
Thanks
In the Unix world, to hide files, you make the first character a .
. So when you simply ls
in a directory, you don't see them. You have to use ls -a
or ls -A
to see them.
You can't "ignore them". You can check in your loop to see if the first character is a .
and continue
.
if ('.' == s_dirent->d_name[0]) {
continue;
}
But keep in mind that they are all equal citizens. So there is no reason to skip them. What you might want to skip are the special files .
(current directory) and ..
(parent directory).
精彩评论