如何使用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) {         cout<<" n"<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.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("%sn", 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++获取目录中的文件列表?的详细内容,更多请关注创想鸟其它相关文章!

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

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 22:40:06
下一篇 2025年12月9日 01:30:49

发表回复

登录后才能评论
关注微信