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

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

标准 C++ 没有提供执行此操作的方法。您可以使用系统命令来初始化 ls 命令,如下所示 –

示例

#includeint main () {   char command[50] = "ls -l";   system(command);   return 0;}

登录后复制

输出

这将给出输出 –

-rwxrwxrwx 1 root root  9728 Feb 25 20:51 a.out-rwxrwxrwx 1 root root   131 Feb 25 20:44 hello.cpp-rwxrwxrwx 1 root root   243 Sep  7 13:09 hello.py-rwxrwxrwx 1 root root 33198 Jan  7 11:42 hello.odrwxrwxrwx 0 root root   512 Oct  1 21:40 hydeout-rwxrwxrwx 1 root root    42 Oct 21 11:29 my_file.txt-rwxrwxrwx 1 root root   527 Oct 21 11:29 watch.py

登录后复制

如果您使用的是 Windows,则可以使用 dir 而不是 ls 来显示列表。

示例

您可以使用直接包(https://github.com/dir/ls)。 com/tronkko/dirent)以使用更灵活的 API。您可以按如下方式使用它来获取文件列表

立即学习“C++免费学习笔记(深入)”;

#include #include #include using namespace std;void list_dir(const char *path) {   struct dirent *entry;   DIR *dir = opendir(path);      if (dir == NULL) {      return;   }   while ((entry = readdir(dir)) != NULL) {   cout d_name 

输出

这将给出输出 -

a.outhello.cpphello.pyhello.ohydeoutmy_file.txtwatch.py

登录后复制

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

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

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

(0)
上一篇 2025年3月6日 14:21:26
下一篇 2025年3月6日 14:21:36

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

相关推荐

发表回复

登录后才能评论