我正在尝试查找文件路径中具有“其他执行”权限的所有子文件夹。
我已经尝试使用 strtok(path_str,"/")
来打破路径字符串,但是当使用 stat()
的子目录时在我运行的进程的根目录下,出现“不是文件或文件夹”错误。
关于如何克服这个错误有什么建议吗?
最佳答案
如果路径是 "long/path/to/the/file.txt"
,那么您需要在 "long 上调用
、stat()
""long/path"
、"long/path/to"
和 "long/path/to/the"
.如果您不关心检查这些顺序的顺序,最简单的方法可能是重复使用 strrchr()
:
char *s;
while (s = strrchr(path, '/'))
{
*s = 0;
if (strlen(path) > 0)
stat(path, &statbuf);
else
stat("/", &statbuf);
/* Do something with statbuf */
}
(特殊大小写用于以 /
开头的路径,以检查根目录本身)。
关于c - 如何在 Linux 下的 C 中找到文件路径中子文件夹的权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4561082/