c++ 17 list files in directory code example
Example 1: c++ get files in directory
#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
std::string path = "/path/to/directory";
for (const auto & entry : fs::directory_iterator(path))
std::cout << entry.path() << std::endl;
}
Example 2: get files in directory c++
#include <dirent.h>
std::vector<std::string> GetRecords()
{
std::vector<std::string> files;
struct dirent *entry;
DIR *dir = opendir(record_dir_path.c_str());
if (dir == NULL)
{
return files;
}
while ((entry = readdir(dir)) != NULL)
{
files.push_back(entry->d_name);
}
closedir(dir);
return files;
}