filename: .
filename: ..
//Open images directory
while (( = readdir()) !== false)
{
}
closedir();
?>
The output of the code above could be:
上述代码将输出下面的结果:
filename: .
filename: ..
filename: cat.gif
filename: dog.gif
filename: food
filename: horse.gif
LinuxC中
头文件
#include
#include
函数原型
DIR* opendir (const char * path );
功能
打开一个目录,在失败的时候返回一个空的指针。
使用实例:
#include
#include
int main(int argc,char** argv)
{
DIR *dirptr = NULL;
struct dirent *entry;
if(argc<2)
{
return 1;
}
if(argc>2)
{
return 1;
}
if((dirptr = opendir(argv[1])) == NULL)
{
return 1;
}
else
{
while (entry = readdir(dirptr))
{
}
closedir(dirptr);
}
return 0;
}
Perl中
函数原型
opendir DIRHANDLE,EXPR;
功能说明
打开目录句柄。
DIRHANDLE:打开的目录句柄,供后续处理使用;
EXPR:指定的待打开的目录路径。
Perl Manualpage :
Opens a directory named EXPR for processing by
readdir, telldir, seekdir,
rewinddir, and
closedir. Returns true if successful. DIRHANDLE may be an expression whose value can be used as an indirect dirhandle, usually the real dirhandle name. If DIRHANDLE is an undefined scalar variable (or array or hash element), the variable is assigned a reference to a new anonymous dirhandle; that is, it's autovivified. DIRHANDLEs have their own namespace separate from FILEHANDLEs.
示例
#!/usr/bin/perl -w
while(( = readdir(DIR))){
### do something ###
}
closedir(DIR);