windows遍历文件夹代码

#include
#include
#include
#include
using namespace std;

//遍历文件夹函数
void traverseFolder(char *pszDir, set<string> &fileSet)
{
    WIN32_FIND_DATA fileData;
	
    HANDLE hFind = ::FindFirstFile(pszDir, &fileData);
    if (INVALID_HANDLE_VALUE == hFind) {
        return;
    }
	
    while (TRUE) {
		string filename(fileData.cFileName);
        fileSet.insert(filename);
        if (!::FindNextFile(hFind, &fileData)) {
            break;
        }
    }
	
    ::FindClose(hFind);
}


int main()
{
	set<string> myFolder;
	char *folderName = "D:\\doc\\*";//*是通配符
	traverseFolder(folderName, myFolder);
	
	for(set<string>::iterator iter = myFolder.begin(); iter != myFolder.end(); ++iter)
	{
		cout<<*iter<cout<<"------------"<return 0;
}

你可能感兴趣的:(windows遍历文件夹代码)