| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1744 人关注过本帖
标题:文件列表不显示出来
只看楼主 加入收藏
chaw899
Rank: 2
等 级:禁止访问
帖 子:48
专家分:11
注 册:2018-11-29
结帖率:88.89%
收藏
 问题点数:0 回复次数:3 
文件列表不显示出来
#include <iostream>
#include <vector>
#include <string>
#include <dirent.h>
using namespace std;
 
void SplitString(const std::string& s, std::vector<std::string>& v, const

std::string& c)
{
  std::string::size_type pos1, pos2;
  pos2 = s.find(c);
  pos1 = 0;
  while(std::string::npos != pos2)
  {
    v.push_back(s.substr(pos1, pos2-pos1));
 
    pos1 = pos2 + c.size();
    pos2 = s.find(c, pos1);
  }
  if(pos1 != s.length())
    v.push_back(s.substr(pos1));
}
 
void readFileList(string path, vector<string> &files, string ext){
    struct dirent *ptr;
    DIR *dir;
    dir=opendir(path.c_str());
    while((ptr=readdir(dir))!=NULL)
    {
        if(ptr->d_name[0] == '.')
            continue;
        std::vector<std::string> strVec;
        SplitString(ptr->d_name, strVec, ".");
 
        if(strVec.size() >=2)
        {
            string p;
            if(strVec[strVec.size()-1].compare(ext) == 0)
                files.push_back(p.assign(path).append("/").append(ptr->d_name));
        }
        else{
            string p;
            readFileList(p.assign(path).append("/").append(ptr->d_name), files,

ext);
        }
    }
    closedir(dir);
}
 
int main(int argc, char * argv[])
{
    string PATH = "I:/185";
    vector<string> files;
    readFileList(PATH, files, "tu.*");  // 本意想显示tu开头的的文件列表没有显示
    readFileList(PATH, files, "png");   // 这句可以显示扩展名png的列表
    for (int i = 0; i < files.size(); ++i)
    {
        cout << files[i] << endl;
    }
    return 0;
}

搜索更多相关主题的帖子: 列表 string std path files 
2019-06-20 21:42
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
if(strVec[strVec.size()-1].compare(ext) == 0)
这是要求文件扩展名必须是“tu.*”才行呀
代码抄的吧,抄完了也没看一样吧
2019-06-21 09:05
chaw899
Rank: 2
等 级:禁止访问
帖 子:48
专家分:11
注 册:2018-11-29
收藏
得分:0 
回复 2楼 rjsp
怎么写才能支持通佩符呢
2019-06-21 14:50
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
回复 3楼 chaw899
linux 用 fnmatch
windows 用 PathMatchSpec
2019-06-21 15:10
快速回复:文件列表不显示出来
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025430 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved