opendir,wopendir
dirent.h:インデックス への移動
ヘッダーファイル
dirent.h
カテゴリ
ディレクトリ制御ルーチン
プロトタイプ
DIR *opendir(const char *dirname);
wDIR *wopendir(const wchar_t *dirname);
説明
読み出し用にディレクトリストリームを開きます。
opendir は,POSIX 準拠の UNIX で使用できます。
opendir 関数は,読み出し用にディレクトリストリームを開きます。dirname は,読み取るディレクトリの名前です。ストリームは,ディレクトリ内の最初のエントリを読み取るように設定されます。
ディレクトリストリームは,dirent.h で定義された DIR 構造体で表されます。この構造体は,ユーザーがアクセスできるフィールドを含みません。複数のディレクトリストリームを開き,同時に読み取ることができます。ディレクトリストリームの読み取り中に,ディレクトリエントリを作成または削除できます。
readdir 関数を使用して,ディレクトリストリームから連続的にエントリを読み取ります。closedir 関数を使用して,不要になったディレクトリストリームを削除します。
戻り値
成功した場合,opendir は,ディレクトリストリームを指すポインタを返します。このポインタは,readdir,rewinddir,および closedir の呼び出しに使用できます。
エラーが発生した場合(ディレクトリを開くことができない場合)は NULL を返し,グローバル変数に次の値を設定します。
ENOENT |
ディレクトリが存在しない |
ENOMEM |
DIR オブジェクトを割り当てるメモリが不足している |
例
/* opendir.c - opendir(),readdir(),closedir() をテストします */
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
void scandir(char *dirname)
{
DIR *dir;
struct dirent *ent;
printf("First pass on '%s':\n",dirname);
if ((dir = opendir(dirname)) == NULL)
{
perror("Unable to open directory");
exit(1);
}
while ((ent = readdir(dir)) != NULL)
printf("%s\n",ent->d_name);
printf("Second pass on '%s':\n",dirname);
rewinddir(dir);
while ((ent = readdir(dir)) != NULL)
printf("%s\n",ent->d_name);
if (closedir(dir) != 0)
perror("Unable to close directory");
}
void main(int argc,char *argv[])
{
if (argc != 2)
{
printf("usage: opendir dirname\n");
exit(1);
}
scandir(argv[1]);
exit(0);
}
移植性
POSIX | Win32 | ANSI C | ANSI C++ | |
---|---|---|---|---|
opendir |
+ |
+ |
||
wopendir |