Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does '.' signify in c as argument to opendir?

"This is the code. What is the meaning of '.' inside "opendir('.')" Does it specifies to the current directory?"

#include <stdio.h> 
#include <dirent.h> 
int main(void) 
{ 
struct dirent *de;  // Pointer for directory entry 

// opendir() returns a pointer of DIR type.  
DIR *dr = opendir("."); 

if (dr == NULL)  // opendir returns NULL if couldn't open directory 
{ 
    printf("Could not open current directory" ); 
    return 0; 
} 


while ((de = readdir(dr)) != NULL) 
        printf("%s\n", de->d_name); 

closedir(dr);     
return 0; 
} 
like image 266
H1rouge Avatar asked Dec 19 '25 21:12

H1rouge


1 Answers

This isn't really programming-related, but more OS-related.

On most operating systems the filesystem have the concept of parent directory and current directory.

The parent directory is commonly using the notation .., while the current directory is using the notation ..

So what opendir(".") will do is to open the current directory.


And as mentioned in a comment, the "current" directory doesn't have to be the directory where the executable program is residing. It's the current working directory for the process, which could be different. It depends on how and where the program was started, and if the program changes its own working directory (which the program in the question doesn't).

like image 60
Some programmer dude Avatar answered Dec 21 '25 14:12

Some programmer dude