Danny
Danny

Reputation: 9614

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? I've looked around and I see people using dirent.h but I need to download it(i think).

Thanks

P.S. I've looked at the fstream, but thats only for reading and outputting files as far as i know.

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. THIS IS FOR WINDOWS ASWELL

Upvotes: 2

Views: 3796

Answers (4)

Akash das
Akash das

Reputation: 61

Using namespace std::filesystem is available in visual studio 2017 you have in #include

#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;
			
	}
	
}

Upvotes: 2

rve
rve

Reputation: 6045

Since others already mentioned boost::filesystem there are also other alternatives. Almost every C++ framework has some way to list directories and files. Like wxWidgets or poco and there are many more.

Regarding dirent.h. It is a standard C Posix library so on Posix compatible systems it should be available. For Windows you can also get it here and it includes instructions on how to use it.

After your edit:

On Windows you can use things like FindFirstFile (example here) and then you don't have to download anything. But it only works on Windows. It is not build in C++.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258548

You can use Boost Filesystem library.

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. For windows you can use CFileFind class from MFC.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409136

How about Boost::Filesystem? Supports directory iteration and is portable.

Upvotes: 3

Related Questions