How can I get the list of files in a directory using C or C++? -
how can determine list of files in directory inside c or c++ code?
i'm not allowed execute 'ls' command , parse results within program.
in small , simple tasks not use boost, use dirent.h available windows:
dir *dir; struct dirent *ent; if ((dir = opendir ("c:\\src\\")) != null) { /* print files , directories within directory */ while ((ent = readdir (dir)) != null) { printf ("%s\n", ent->d_name); } closedir (dir); } else { /* not open directory */ perror (""); return exit_failure; } it small header file , of simple stuff need without using big template-based approach boost(no offence, boost!). googled , found links here author of windows compatibility layer toni ronkko. in unix standard-header.
update 2017:
in c++17 there official way list files of file system: std::filesystem. there excellent answer shreevardhan below source code:
#include <string> #include <iostream> #include <filesystem> namespace fs = std::filesystem; int main() { std::string path = "path_to_directory"; (auto & p : fs::directory_iterator(path)) std::cout << p << std::endl; } consider upvoting answer, if using c++17 approach.
Comments
Post a Comment