首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

VC平台下搜寻硬盘中的文件或文件夹有哪些路子,大家给支支招

2013-09-06 
VC平台下搜索硬盘中的文件或文件夹有哪些路子,大家给支支招如题,大家都教授一下自己心得,晚辈在这里谢谢了

VC平台下搜索硬盘中的文件或文件夹有哪些路子,大家给支支招
如题,大家都教授一下自己心得,晚辈在这里谢谢了! 文件搜索 vc
[解决办法]

一、遍历当前目录下指定的文件(win下的)
#include <io.h>
//查找dir路径下所有文件(dir包括配置符,如c:\*.txt)
//ishavedir表示返回的文件名是否包含全路径,true为返回全路径的文件名,false只返回文件名
vector<string> FindAllFile(const char* dir,bool ishavedir=false)
{
    _finddata_t file;
    vector<string> file_list;
    long lf;
    if((lf = _findfirst(dir, &file))==-1l) { //_findfirst返回的是long型; long __cdecl _findfirst(const char *, struct _finddata_t *)
        return file_list;
    } else {
        do {
            if (ishavedir) {
                string tmppath=dir;
                int index=tmppath.find_last_of("*.")-1;
                tmppath=tmppath.substr(0,index)+file.name;
                file_list.push_back(tmppath);
            } else {
                file_list.push_back(file.name);
            }
        } while (_findnext( lf, &file ) == 0);//int _findnext(long, struct _finddata_t *);如果找到下个文件的名字成功的话就返回0,否则返回-1
        _findclose(lf);
        return file_list;
    }
}

热点排行