Linux C中的opendir()

頭文件

  #include<sys/types.h>

  #include<dirent.h>

函數原型

  DIR* opendir (const char * path );

功能

  打開一個目錄,在失敗的時候返回一個空的指針。
  使用實例:
  1. #include <stdio.h>  
  2. #include <dirent.h>  
  3. int main(void)  
  4. {  
  5. DIR *dirptr = NULL;  
  6. struct dirent *entry;  
  7. if((dirptr = opendir(argv[1])) == NULL)  
  8. {  
  9. printf{\"open dir !\"};  
  10. return 1;  
  11. }  
  12. else  
  13. {  
  14. while (entry = readdir(dirptr))  
  15. {  
  16. printf(\"%s\\n\", entry->d_name);/* 打印出該目錄下的所有內容 */  
  17. }  
  18. closedir(dirptr);  
  19. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章