File -x opendir()

头文件

#include<sys/types.h>
#include<dirent.h>
函数原型

DIR* opendir (const char * path );
功能

打开一个目录,在失败的时候返回一个空的指针。


使用实例:

#include <stdio.h>
#include <dirent.h>
int main(int argc,char** argv)
{
DIR *dirptr = NULL;
struct dirent *entry;
if(argc<2)
{
printf("the command need a dirname
");
return 1;
}
if(argc>2)
{
printf("the program can only deal with one dir at once
");
return 1;
}
if((dirptr = opendir(argv[1])) == NULL)
{
printf("open dir error !
");
return 1;
}
else
{
while (entry = readdir(dirptr))
{
printf("%s
", entry->d_name);
}
closedir(dirptr);
}
return 0;
}

目录结构体 -》 http://www.cnblogs.com/pencil-zh/p/4506086.html

原文地址:https://www.cnblogs.com/pencil-zh/p/4505908.html