How can I get the list of files in a directory using C or C++?

前端 未结 27 3245
情书的邮戳
情书的邮戳 2020-11-21 05:30

How can I determine the list of files in a directory from inside my C or C++ code?

I\'m not allowed to execute the ls command and parse the results from

27条回答
  •  爱一瞬间的悲伤
    2020-11-21 06:22

    I recommend using glob with this reusable wrapper. It generates a vector corresponding to file paths that fit the glob pattern:

    #include 
    #include 
    using std::vector;
    
    vector globVector(const string& pattern){
        glob_t glob_result;
        glob(pattern.c_str(),GLOB_TILDE,NULL,&glob_result);
        vector files;
        for(unsigned int i=0;i

    Which can then be called with a normal system wildcard pattern such as:

    vector files = globVector("./*");
    

提交回复
热议问题