プログラムでディレクトリを取得し、そのディレクトリの内容を出力して、各項目がディレクトリであるかどうかを示します。ファイル F1.txt および F2.txt とフォルダー D1、D2、および D3 を含むディレクトリを指定すると、次のように出力されます。
F1.txt はディレクトリではありません
F2.txt はディレクトリではありません
D1 はディレクトリです
D2 はディレクトリです
D3 はディレクトリです
char* curr[100];
DIR* dirp = opendir(name);
struct dirent* x;
struct stat fstat;
//go to each file til readdir gives NULL
while((x = readdir(dirp)) != NULL) {
//store name of file
curr[0] = (x -> d_name);
//ignore files starting with "."
if(*curr[0] == '.')
continue;
//set status
stat(curr[0], &fstat);
//print file name
printf("%s", *curr);
//check if it's a directory and print result
if(S_ISDIR(fstat.st_mode))
printf(" is directory\n");
else
printf(" is not directory\n");
}
これは、すべてのファイルがディレクトリではないことを示しています。「.」で始まるファイルを無視する部分を削除すると、F1.txt、F2.txt、および D1 はディレクトリではなく、.、D2、D3、および .. がディレクトリであることが示されます (この順序で)。問題はマクロの使用ではなく stat 呼び出しにあると思われますが、ここではかなり混乱しているのでわかりません。