HANDLE WINAPI FindFirstFile( _In_ LPCTSTR lpFileName, _Out_ LPWIN32_FIND_DATA lpFindFileData );
BOOL WINAPI FindNextFile( _In_ HANDLE hFindFile, _Out_ LPWIN32_FIND_DATA lpFindFileData );
HANDLE hFile; LPCTSTR lpFileName = L".\\*.*"; //指定搜索目录和文件类型,如搜索d盘的音频文件可以是"D:\\*.mp3" WIN32_FIND_DATA pNextInfo; //搜索得到的文件信息将储存在pNextInfo中; hFile = FindFirstFile(lpFileName,&pNextInfo);//请注意是 &pNextInfo , 不是 pNextInfo; if(hFile == INVALID_HANDLE_VALUE) { //搜索失败 exit(-1); } while(FindNextFile(hFile,&pNextInfo)) { if(pNextInfo.cFileName[0] == '.')//过滤.和.. continue; cout<<"得到文件:"<<pNextInfo.cFileName<<endl; }
头文件 #include <windows.h>
HANDLE file; LPCTSTR lpFileName="浙江黄大豆\\*.*"; WIN32_FIND_DATA pNextInfo; file=FindFirstFile(lpFileName, &pNextInfo); if (file == INVALID_HANDLE_VALUE) { cout<<"搜索失败"<<endl; system("pause"); exit(0); } while (FindNextFile(file, &pNextInfo)) { if (pNextInfo.cFileName[0] == '.') continue; cout<<"得到文件:"<<pNextInfo.cFileName<<endl; }
得到文件夹(黑豆)中的所有.jpg结尾的文件
void findfile(void) { /* string path; cout<<"inpt(xx\\xx\\*.*): "<<endl; cin>>path; cout<<path<<endl; */ HANDLE file; LPCTSTR lpFileName="黑豆\\*.jpg";//path.c_str(); WIN32_FIND_DATA pNextInfo; file=FindFirstFile(lpFileName, &pNextInfo); if (file == INVALID_HANDLE_VALUE) { cout<<"搜索失败"<<endl; system("pause"); exit(0); } cout<<"得到文件:"<<pNextInfo.cFileName<<endl;//请注意这句 int count=1; while (FindNextFile(file, &pNextInfo)) { if (pNextInfo.cFileName[0] == '.') continue; cout<<"得到文件:"<<pNextInfo.cFileName<<endl; count++; } cout<<"count = "<<count<<endl; }