我怎样才能使用C或C ++目录中的文件列表? [英] How can I get the list of files in a directory using C or C++?

查看:137
本文介绍了我怎样才能使用C或C ++目录中的文件列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能确定文件的清单目录中的从我的C或C ++ code里面呢?

How can I determine the list of files in a directory from inside my C or C++ code?

我不能执行'ls'的命令,并从我的程序中分析结果。

I'm not allowed to execute the 'ls' command and parse the results from within my program.

推荐答案

在小而简单的任务,我不使用升压,我使用 dirent.h 这也适用于Windows:

In small and simple tasks I do not use boost, I use dirent.h which is also available for windows:

DIR *dir;
struct dirent *ent;
if ((dir = opendir ("c:\\src\\")) != NULL) {
  /* print all the files and directories within directory */
  while ((ent = readdir (dir)) != NULL) {
    printf ("%s\n", ent->d_name);
  }
  closedir (dir);
} else {
  /* could not open directory */
  perror ("");
  return EXIT_FAILURE;
}

这只是一个小的头文件,并做你最需要简单的东西,而不喜欢使用升压一个大基于模板的方法(没有进攻,我喜欢刺激!)。
我一派,这里发现了一些链接
在Windows兼容层的作者是托尼Ronkko。在Unix它是一个标准的头。

It is just a small header file and does most of the simple stuff you need without using a big template-based approach like boost(no offence, I like boost!). I googled and found some links here The author of the windows compatibility layer is Toni Ronkko. In Unix it is a standard-header.

这篇关于我怎样才能使用C或C ++目录中的文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆