在 CentOS 系統中,你可以使用 readdir
函數來讀取目錄中的文件
以下是一個簡單的示例,展示了如何使用 readdir
過濾掉特定文件:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
int main(int argc, char *argv[]) {
DIR *dir;
struct dirent *entry;
if (argc != 2) {
fprintf(stderr, "Usage: %s <directory>\n", argv[0]);
return EXIT_FAILURE;
}
dir = opendir(argv[1]);
if (dir == NULL) {
perror("opendir");
return EXIT_FAILURE;
}
while ((entry = readdir(dir)) != NULL) {
// 過濾掉特定文件,例如 ".DS_Store"
if (strcmp(entry->d_name, ".DS_Store") != 0) {
printf("%s\n", entry->d_name);
}
}
closedir(dir);
return EXIT_SUCCESS;
}
將此代碼保存為 filter_files.c
,然后使用以下命令編譯:
gcc filter_files.c -o filter_files
現在,你可以運行編譯后的程序,傳遞一個目錄作為參數,它將輸出該目錄中的所有文件,除了 .DS_Store
文件:
./filter_files /path/to/directory
請注意,這個示例僅用于演示目的。在實際應用中,你可能需要根據你的需求對代碼進行調整。