现在的位置: 首页 > 综合 > 正文

Linux C中的opendir()

2014年11月08日 ⁄ 综合 ⁄ 共 415字 ⁄ 字号 评论关闭

头文件

  #include<sys/types.h>

  #include<dirent.h>

函数原型

  DIR* opendir (const char * path );

功能

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

  使用实例:

  #include <stdio.h>
  #include <dirent.h>
  int main(void)
  {
  DIR *dirptr = NULL;
  struct dirent *entry;
  if((dirptr = opendir(argv[1])) == NULL)
  {
  printf{\"open dir !\"};
  return 1;
  }
  else
  {
  while (entry = readdir(dirptr))
  {
  printf(\"%s\\n\", entry->d_name);/* 打印出该目录下的所有内容 */
  }
  closedir(dirptr);
  }
  return 0;
  }

抱歉!评论已关闭.