简体   繁体   English

C ++搜索目录和文件

[英]C++ search directories and files

Can someone tell me if there is a library built in C++ that I can use to get a list of files and directories? 有人可以告诉我是否有一个C ++内置的库,可以用来获取文件和目录的列表吗? I've looked around and I see people using dirent.h but I need to download it(i think). 我环顾四周,看到有人在使用dirent.h,但我需要下载它(我认为)。

Thanks 谢谢

PS I've looked at the fstream, but thats only for reading and outputting files as far as i know. PS我看过fstream,但据我所知,那仅用于读取和输出文件。

FORGOT TO MENTION. 忘记提及。 I DO NOT WANT TO DOWNLOAD ANYTHING, I JUST WANT TO SEE IF THERE IS A LIBRARY THAT IS BUILT WITHIN C++ THAT I CAN USE STRAIGHT OFF THE BAT. 我不想下载任何东西,我只想看看是否有C ++内置的库可以直接使用BAT。 THIS IS FOR WINDOWS ASWELL 这也适用于WINDOWS

How about Boost::Filesystem ? Boost :: Filesystem怎么样? Supports directory iteration and is portable. 支持目录迭代并且可移植。

You can use Boost Filesystem library. 您可以使用Boost Filesystem库。

http://www.boost.org/doc/libs/1_31_0/libs/filesystem/doc/index.htm http://www.boost.org/doc/libs/1_31_0/libs/filesystem/doc/index.htm

Some nice samples are also provided at the link. 链接上还提供了一些不错的示例。

EDIT: 编辑:

Without downloading a 3rd party library, there is no portable way to do it. 如果不下载第3方库,就没有便携式方法。 For windows you can use CFileFind class from MFC. 对于Windows,您可以使用MFC中的CFileFind类。

Using namespace std::filesystem is available in visual studio 2017 you have in #include #include中使用的Visual Studio 2017中提供了使用命名空间std :: filesystem的功能

 #include <iostream> #include <iostream> #include <fstream> #include <filesystem> #include <chrono> #include <thread> #include <functional> namespace fs = std::filesystem; void Files_in_Directory(); fs::path path_Copy_Directory = "E:\\Folder"; fs::path path_Paste_Directory = "E:\\Folder1"; int main() { Files_in_Directory() return 0; } void Files_in_Directory() { for (const auto & entry : fs::directory_iterator(path_Copy_Directory)) { std::cout << entry.path() << std::endl; } } 

Since others already mentioned boost::filesystem there are also other alternatives. 由于其他人已经提到了boost :: filesystem ,因此还有其他选择。 Almost every C++ framework has some way to list directories and files. 几乎每个C ++框架都有某种列出目录和文件的方法。 Like wxWidgets or poco and there are many more. wxWidgetspoco一样 ,还有更多。

Regarding dirent.h . 关于dirent.h It is a standard C Posix library so on Posix compatible systems it should be available. 这是一个标准的C Posix库,因此在与Posix兼容的系统上应该可用。 For Windows you can also get it here and it includes instructions on how to use it. 对于Windows,您也可以在此处获得它,其中包括有关如何使用它的说明。

After your edit: 编辑后:

On Windows you can use things like FindFirstFile ( example here ) and then you don't have to download anything. 在Windows上,您可以使用诸如FindFirstFile之类的东西( 此处为示例 ),然后您无需下载任何内容。 But it only works on Windows. 但它仅适用于Windows。 It is not build in C++. 它不是用C ++构建的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM