码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-12-09 12:19:33      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:null   c++   find   stream   pre   文件句柄   句柄   pat   oid   

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()

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

标签:null   c++   find   stream   pre   文件句柄   句柄   pat   oid   

原文地址:https://www.cnblogs.com/sinicheveen/p/12009831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!