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;
}
}