我正在创建一个列出目录内容的简单 C 程序。有谁知道如何只列出隐藏的?以下代码从目录中提取每个文件并且工作完美,但我只需要隐藏文件。谢谢。
最佳答案
在 GNU/Linux 上,隐藏文件以点开头。
#include <string.h>
int is_hidden(const char *name)
{
return name[0] == '.' &&
strcmp(name, ".") != 0 &&
strcmp(name, "..") != 0);
}
要检查文件是否为只读,使用 stat
函数可能是个好主意。
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int is_readonly(const char *name)
{
struct stat buf;
stat(name, &buf);
return buf->st_mode & /* ... */;
}
关于C程序列出隐藏文件和只读文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14663548/