c++读取文件夹中所有文件名代码

//头文件
#include 
#include 
#include 
#include 
#include 
 
using namespace std;
 
 
void GetFileNames(string path,vector<string>& filenames)
{
    DIR *pDir;
    struct dirent* ptr;
    if(!(pDir = opendir(path.c_str())))
        return;
    while((ptr = readdir(pDir))!=0) {
        if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0)
            filenames.push_back(path + "/" + ptr->d_name);
    }
    closedir(pDir);
}
 
int main() {
    vector<string> file_name;
    string path = "/home/image";
 
    GetFileNames(path, file_name);
 
    for(int i = 0; i <file_name.size(); i++)
    {
        cout<<file_name[i]<<endl;
    }
 
    return 0;
}
参考此贴:
https://blog.csdn.net/weixin_41770169/article/details/94566944

你可能感兴趣的:(c++,开发语言)