C++ 读取一个文件下所有文件的文件名

Windows:

#include<iostream>
#include<string>
#include <io.h>
void readFileNameInDir(IN string strDir, INOUT vector<string>& vFileFullPath)
{
       long handle;    //文件句柄
       struct _finddata_t fileInfo;    //文件结构体
       handle = _findfirst(strDir.c_str(), &fileInfo);    //第一次查找,获取文件句柄
       while (!_findnext(handle, &fileInfo))
       {
              vFileFullPath.push_back(fileInfo.name)
       }
       _findclose(handle);
}    //readFileNameInDir()

Linux:

#include <string>
#include <vector>
#include <dirent.h>

void readFileNameInDir(IN string strDir, INOUT vector<string>& vFileFullPath)
{
       struct dirent* pDirent;
       DIR* pDir = opendir(strDir.c_str());
       if (pDir != NULL)
       {
              while ((pDirent = readdir(pDir)) != NULL)
              {
                     string strFileName = pDirent->d_name;
                     string strFileFullPath = strDir + "/" + strFileName;
                     vFileFullPath.push_back(strFileFullPath);
              }
              vFileFullPath.erase(vFileFullPath.begin(), vFileFullPath.begin() +  2);    //前两个存储的是当前路径和上一级路径,所以要删除
       }
}    //readFileNameInDir()

猜你喜欢

转载自www.cnblogs.com/sinicheveen/p/12009831.html