获取目录下所有的文件

来源:互联网 发布:淘宝不给退货怎么办 编辑:程序博客网 时间:2024/05/28 22:10
/******获取目录下所有的文件*****/#include#include#include#include#include#includeusing std::cout;using std::endl;using std::string;using std::vector;enum { RETURN_ERROR = -1, RETURN_OK };void GetFiles(const string& strDir,vector& vecFile){_finddata_t file;long lf;if (RETURN_ERROR == (lf = _findfirst(strDir.c_str(), &file))){cout << "not found" << strDir << endl;}else{while (RETURN_OK == _findnext(lf, &file)){if (RETURN_OK == strcmp(file.name, ".") || RETURN_OK == strcmp(file.name, "..")){continue;}if (file.attrib&_A_SUBDIR){string strTmpDir(strDir.substr(0, strDir.length() - 1) + file.name + "\\*");GetFiles(strTmpDir, vecFile);}else{vecFile.push_back(file.name);}}}}int main(){char szPath[MAX_PATH] = { 0 };_getcwd(szPath, MAX_PATH);strncat_s(szPath, "\\*", MAX_PATH);vectorvecFile;GetFiles(szPath, vecFile);for (auto&it : vecFile){cout << it << endl;}system("pause");return 0;}
阅读全文
0 0