如何使用C或C++获取目录中的文件列表?

如何使用c或c++获取目录中的文件列表?

让我们考虑以下 C++ 示例代码来获取目录中的文件列表

算法

Begin   Declare a poniter dr to the DIR type.   Declare another pointer en of the dirent structure.   Call opendir() function to open all file in present directory.   Initialize dr pointer as dr = opendir(".").   If(dr)      while ((en = readdir(dr)) != NULL)         print all the file name using en->d_name.      call closedir() function to close the directory.End.

登录后复制

示例

#include #include #include using namespace std;int main(void) {   DIR *dr;   struct dirent *en;   dr = opendir("."); //open all directory   if (dr) {      while ((en = readdir(dr)) != NULL) {         coutd_name; //print all directory name      }      closedir(dr); //close all directory   }   return(0);}

登录后复制

输出

BINSEARC.CBINTREE (1).CBINTREE.CBTREE.CBUBBLE.Cc.txtfile3.txtHEAP.CHEAPSORT.CHLINKLST.CINSERTIO.CLINKLIST.CLINKLST.CLLIST1.Cplayers.cppPolarRect.cppQUEUE.C

登录后复制

示例

#include #include int main(void) {   DIR *dr;   struct dirent *en;   dr = opendir("."); //open all or present directory   if (dr) {      while ((en = readdir(dr)) != NULL) {         printf("%s", en->d_name); //print all directory name      }      closedir(dr); //close all directory   }   return(0);}

登录后复制

输出

BINSEARC.CBINTREE (1).CBINTREE.CBTREE.CBUBBLE.Cc.txtfile3.txtHEAP.CHEAPSORT.CHLINKLST.CINSERTIO.CLINKLIST.CLINKLST.CLLIST1.C

登录后复制

以上就是如何使用C或C++获取目录中的文件列表?的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2580679.html

(0)
上一篇 2025年3月6日 13:48:14
下一篇 2025年3月3日 05:54:40

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论