Loose-Info.com
Last Update 2012/07/05
TOP - C言語 - dirent.h - readdir()

※ この関数のコード例・結果に関してはOS X v10.6でのみ検証しています。
その他の環境下では、実行不能な場合も確認されています。

引数のポインタによって参照されるディレクトリストリームの次のディレクトリエントリ(dirent構造体)へのポインタを返します。

 戻り値1  = readdir(  引数1  )

戻り値1 :
struct dirent *
実行が成功した場合
dirent構造体へのポインタ
エラーが発生した場合
NULL

dirent構造体のメンバ(主なもの)
__uint16_t  d_reclen
このレコードの長さ
__uint8_t  d_namlen
d_nameの文字列の長さ
char  d_name[]
ディレクトリ名
引数1 :
DIR *
ディレクトリストリームへのポインタ

(例)
#include <stdio.h> #include <sys/stat.h> #include <dirent.h> int main () { FILE *fp; struct dirent *dirst; mkdir("test_dir_01", 0777); fp = fopen("test_dir_01/test_file_01.txt", "w"); fclose(fp); DIR *dp = opendir("test_dir_01"); while((dirst = readdir(dp)) != NULL) { printf("レコード長 : [%d]\n", dirst->d_reclen); printf("ディレクトリ名の文字列の長さ : [%d]\n", dirst->d_namlen); printf("ディレクトリ名 : [%s]\n\n", dirst->d_name); } closedir(dp); remove("test_dir_01/test_file_01.txt"); remove("test_dir_01"); return 0; }

実行結果
レコード長 : [24] ディレクトリ名の文字列の長さ : [1] ディレクトリ名 : [.] レコード長 : [24] ディレクトリ名の文字列の長さ : [2] ディレクトリ名 : [..] レコード長 : [44] ディレクトリ名の文字列の長さ : [16] ディレクトリ名 : [test_file_01.txt]