readdir 是一个用于读取目录内容的函数,通常在 C 语言中使用。它会返回目录中的下一个条目(文件或子目录),直到目录中的所有条目都被读取完毕。要处理文件属性,您可以使用 stat 函数来获取文件的详细信息。

以下是一个简单的示例,展示了如何使用 readdir 和 stat 函数来处理文件属性:
#include <stdio.h>#include <stdlib.h>#include <dirent.h>#include <sys/stat.h>#include <string.h>int main() {DIR *dir;struct dirent *entry;struct stat file_stat;char path[1024];dir = opendir("."); // 打开当前目录if (dir == NULL) {perror("opendir");return EXIT_FAILURE;}while ((entry = readdir(dir)) != NULL) {// 跳过当前目录(.)和上级目录(..)条目if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {continue;}// 构建文件的完整路径snprintf(path, sizeof(path), "./%s", entry->d_name);// 获取文件的详细信息if (stat(path, &file_stat) == -1) {perror("stat");continue;}// 处理文件属性printf("File: %sn", entry->d_name);printf("Size: %ld bytesn", file_stat.st_size);printf("Last modified: %s", ctime(&file_stat.st_mtime));}closedir(dir);return EXIT_SUCCESS;}这个示例程序首先打开当前目录,然后使用 readdir 函数读取目录中的每个条目。对于每个条目,它使用 stat 函数获取文件的详细信息,包括文件大小和最后修改时间。然后,它将这些信息打印到控制台。
注意:在实际应用中,您可能需要根据需要处理更多的文件属性,例如文件类型、权限等。这可以通过检查 file_stat 结构中的相应字段来实现。