Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1.glob
- ------
- ```c++
- #include <glob.h>
- glob_t glob_result;
- glob("/your_directory/*",GLOB_TILDE,NULL,&glob_result);
- for(unsigned int i=0; i<glob_result.gl_pathc; ++i){
- cout << glob_result.gl_pathv[i] << endl;
- }
- globfree(&glob_result);
- ```
- **vector封装:**
- ```c++
- #include <glob.h>
- #include <vector>
- #include <string>
- inline std::vector<std::string> glob(const std::string& pat){
- using namespace std;
- glob_t glob_result;
- glob(pat.c_str(),GLOB_TILDE,NULL,&glob_result);
- vector<string> ret;
- for(unsigned int i=0;i<glob_result.gl_pathc;++i){
- ret.push_back(string(glob_result.gl_pathv[i]));
- }
- globfree(&glob_result);
- return ret;
- }
- ```
- 2.c++17
- -------
- ```c++
- #include <string>
- #include <iostream>
- #include <filesystem>
- namespace fs = std::filesystem;
- int main()
- {
- std::string path = "path_to_directory";
- for (auto & p : fs::directory_iterator(path))
- std::cout << p << std::endl;
- }
- ```
- 3.boost
- -------
- ```c++
- #include <string>
- #include <iostream>
- #include <boost/filesystem.hpp>
- using namespace std;
- using namespace boost::filesystem;
- int main()
- {
- path p("D:/AnyFolder");
- for (auto i = directory_iterator(p); i != directory_iterator(); i++)
- {
- if (!is_directory(i->path())) //we eliminate directories
- {
- cout << i->path().filename().string() << endl;
- }
- else
- continue;
- }
- }
- ```
- 4. dirent
- -------
- ```c++
- #include <dirent.h>
- DIR *dir;
- struct dirent *ent;
- if ((dir = opendir ("c:\\src\\")) != NULL) {
- /* print all the files and directories within directory */
- while ((ent = readdir (dir)) != NULL) {
- printf ("%s\n", ent->d_name);
- }
- closedir (dir);
- } else {
- /* could not open directory */
- perror ("");
- return EXIT_FAILURE;
- }
- ```
Add Comment
Please, Sign In to add comment