cfiledirectoryc11opendir

C Code trying to open a valid directory with opendir() ends with error "No such file or directory"


I am not able to figure out whats wrong with this simple code, so looking for C experts to guide here please :) The folder exist and have standard permissions set, even if I try to access D Drive root folder it doesn't work. I am testing this program on Windows 10, using WSL, and compiled program using "cc -std=c11 -o sample sample.c" and code compiled without errors. My code file "sample.c" is in a sub folder "D:\TopFolder\Test"

Here is my code and I am expecting code to list all files and directories under the path specified.

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int main() {
    //const char *path = "D:\\TopFolder"; // this also did not work
    const char *path = "D:\\"; // Specify the directory path
    DIR *directory;
    struct dirent *entry;

    // Open the directory
    directory = opendir(path);
    if (directory == NULL) {
        printf("Attempting to open directory: %s\n", path);
        perror("Failed to open directory");
        return EXIT_FAILURE; // Exit with failure if unable to open directory
    }

    // Read entries from the directory
    printf("Contents of %s:\n", path);
    while ((entry = readdir(directory)) != NULL) {
        // Skip the "." and ".." entries
        if (entry->d_name[0] != '.') {
            printf("%s\n", entry->d_name);
        }
    }

    // Close the directory
    closedir(directory);
    return EXIT_SUCCESS; // Exit successfully
}

"icacls d:" in command line returns the following, if this helps -

d: BUILTIN\\Administrators:(F)  
BUILTIN\\Administrators:(OI)(CI)(IO)(F)  
NT AUTHORITY\\SYSTEM:(F)  
NT AUTHORITY\\SYSTEM:(OI)(CI)(IO)(F)  
NT AUTHORITY\\Authenticated Users:(M)  
NT AUTHORITY\\Authenticated Users:(OI)(CI)(IO)(M)  
BUILTIN\\Users:(RX)  
BUILTIN\\Users:(OI)(CI)(IO)(GR,GE)

Solution