C++ 文件和文件夹判断及访问(后缀过滤)ubuntu

#include <sys/stat.h>
#include <vector>
#include <string>
#include <dirent.h>

struct stat s;

std::string scene = jsonParams["Path"];
    const char* selectedScenePath = scene.c_str();
    std::vector<std::string> scenePaths;
    if (stat(selectedScenePath, &s) == 0){
        if(s.st_mode & S_IFDIR){
            std::cout<<"it's a directory"<<std::endl;
            DIR *dir;
            struct dirent *ptr;
            dir = opendir(selectedScenePath); 
            while((ptr=readdir(dir))!=NULL)
            {
                if(ptr->d_name[0] == '.')
                    continue;
                std::string endWith = ptr->d_name;
                std::cout << "endWith: " << endWith << endWith.substr(0, endWith.length() -4) << std::endl;
                if(endWith.substr(endWith.length() -4, endWith.length()) == ".ply")
                    scenePaths.push_back(scene + "/" + ptr->d_name);
            }
            for(int i=0; i<scenePaths.size(); i++)
                std::cout << "scenePaths: " << scenePaths[i] << std::endl;
            std::cout << std::endl;
        }else if (s.st_mode & S_IFREG){
            std::cout<<"it's a file"<<std::endl;
            scenePaths.push_back(selectedScenePath);
        }else{
            std::cout<<"not file not directory"<<std::endl;
        }
    }else{
        std::cout<<"error, doesn't exist"<<std::endl;
    }

```C++

getcwd获取当前工作目录,类似上一方法

#include  <direct.h>  
#include  <stdio.h> 
 
char   buffer[MAX_PATH];   
getcwd(buffer, MAX_PATH); 

```


版权声明:本文为qingqiqiuwhy原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。