阅读量:2
在 Ubuntu 中,你可以使用 readdir 函数来读取目录内容
以下是一个简单的示例,展示了如何使用 readdir 过滤掉特定文件类型(例如 .txt 文件):
#include
#include
#include
#include
int main() {
DIR *dir;
struct dirent *entry;
dir = opendir("."); // 打开当前目录
if (dir == NULL) {
perror("opendir");
exit(EXIT_FAILURE);
}
while ((entry = readdir(dir)) != NULL) {
// 检查文件名是否以 .txt 结尾
if (strstr(entry->d_name, ".txt") == NULL) {
printf("%s\n", entry->d_name);
}
}
closedir(dir);
return 0;
}
这个程序会列出当前目录下的所有文件和文件夹,但不会显示以 .txt 结尾的文件。你可以根据需要修改 strstr(entry->d_name, ".txt") == NULL 这一行来过滤其他文件类型。
以上就是关于“Ubuntu readdir 如何过滤特定文件类型”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm