我无法弄清楚这个简单的代码有什么问题,所以请寻找 C 专家来指导:) 该文件夹存在并且设置了标准权限,即使我尝试访问 D 驱动器根文件夹它也不起作用。我正在 Windows 10 上使用 WSL 测试该程序,并使用“cc -std=c11 example sample.c”编译程序,并且编译的代码没有错误。我的代码文件“sample.c”位于子文件夹“D:\TopFolder\Test”
这是我的代码,我希望代码列出指定路径下的所有文件和目录。
#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:”返回以下内容,如果这有帮助 -
d:内置\管理员:(F)
内置\管理员:(OI)(CI)(IO)(F)
NT 当局\系统:(F)
NT 权威\系统:(OI)(CI)(IO)(F)
NT AUTHORITY\经过身份验证的用户:(M)
NT AUTHORITY\经过身份验证的用户:(OI)(CI)(IO)(M)
内置\用户:(RX)
内置\用户:(OI)(CI)(IO)(GR,GE)
cc -std=c11 sample sample.c
- 这将尝试使用 sample
作为输入,而不是将结果写入其中。添加-o
:
cc -std=c11 -o sample sample.c
D:\
不为 wsl 所知。请使用 /mnt/d
来代替。