c++ 中的递归文件夹扫描
我想扫描目录树并列出每个目录内的所有文件和文件夹。 我创建了一个程序,可以从网络摄像头下载图像并将其保存在本地。 该程序根据下载图片的时间创建一个文件树。 我现在想扫描这些文件夹并将图像上传到网络服务器,但我不确定如何扫描目录以查找图像。 如果有人可以发布一些示例代码,那将会非常有帮助。
编辑:我在嵌入式 Linux 系统上运行它,并且不想使用 boost
I want to scan a directory tree and list all files and folders inside each directory. I created a program that downloads images from a webcamera and saves them locally. This program creates a filetree based on the time the picture is downloaded. I now want to scan these folders and upload the images to a webserver but I´m not sure how I can scan the directories to find the images.
If anyone could post some sample code it would be very helpful.
edit: I´m running this on an embedded linux system and don´t want to use boost
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请参阅
man ftw
了解简单的“文件树遍历”。 我还在本示例中使用了fnmatch
。(甚至没有经过编译测试,但您明白了。)
这比您自己处理 DIRENT 和递归遍历要简单得多。
为了更好地控制遍历,还有
fts
。 在此示例中,点文件(名称以“.”开头的文件和目录)将被跳过,除非明确传递给程序作为起点。再说一次,它既没有经过编译测试,也没有经过运行测试,但我想我会提到它。
See
man ftw
for a simple "file tree walk". I also usedfnmatch
in this example.(Not even compile-tested, but you get the idea.)
This is much simpler than dealing with
DIRENT
s and recursive traversal yourself.For greater control over traversal, there's also
fts
. In this example, dot-files (files and directories with names starting with ".") are skipped, unless explicitly passed to the program as a starting point.Again, it's neither compile-tested nor run-tested, but I thought I'd mention it.
Boost.Filesystem 允许您做到这一点。 查看文档!
编辑:
如果您使用 Linux 并且不想使用 Boost,则必须使用 Linux 原生 C 函数。 此页面展示了许多有关如何做到这一点的示例。
Boost.Filesystem allows you to do that. Check out the docs!
EDIT:
If you are using Linux and you don't want to use Boost, you will have to use the Linux native C functions. This page shows many examples on how to do just that.
我是老派,没有 ftw() 对我来说! 这很粗糙(自从我直接进行 C 编程以来已经有一段时间了),而且很多东西都是硬编码的,而且我可能搞乱了 strnc*() 函数的长度计算,但你明白了。 顺便说一句,K&R 中有一个类似的例子。
I'm old school, no ftw() for me! This is crude (it's been a while since I did straight C programming), and lots of stuff is hardcoded, and I probably messed up my length calculations for the strnc*() functions, but you get the idea. There's a similar example in K&R btw.
您还可以使用 glob/globfree。
You can also use glob/globfree.
我想如果你可以使用 Qt/Embedded,有 QDir 和 QFileInfo 类
可以帮助你,尽管这取决于你是否会使用 Qt。 问题是你的系统提供哪个API。
I think if you can use Qt/Embedded, there are QDir and QFileInfo classes which
can help you, though it depends if you can use the Qt. The question is which API your system provides.
您将需要使用 dirent.h 中声明的目录函数。 这个维基百科页面描述了它们并包含示例代码。 对于您的应用程序,一旦识别了目录,您将需要再次递归地调用处理函数来处理目录内容。
You will want to use the directory functions declared in dirent.h. This wikipedia page describes them and includes sample code. For your application, once you have identified a directory, you will want to call the processing function again recursively to process the directory contents.