70,037
社区成员
发帖
与我相关
我的任务
分享#include <stdlib.h>
#include <windows.h>
#pragma warning(disable: 4786)
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
//搜索文件名
void ListAllFiles(char *pathName,vector<string> &af)
{
int len = strlen(pathName);
if(pathName==NULL || len<=0)
return;
WIN32_FIND_DATA FindFileData;
HANDLE FileHandle;
string strConv;
char stemp[512];
sprintf(stemp,"%s\\*.*",pathName);
FileHandle = FindFirstFile(stemp,&FindFileData);
if (FileHandle == INVALID_HANDLE_VALUE)
{
return;
}
BOOL bIsDirectory;
BOOL bFinish = FALSE;
char tempPath[MAX_PATH];
while(!bFinish && FileHandle != INVALID_HANDLE_VALUE)
{
bIsDirectory = ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
//. or .. do not handle
if ( bIsDirectory && ( strcmp(FindFileData.cFileName,".") == 0 || strcmp(FindFileData.cFileName,"..") == 0) )
{
bFinish = (FindNextFile(FileHandle, &FindFileData) == FALSE);
continue;
}
strConv = FindFileData.cFileName;
//if file, save it in the vector
if (!bIsDirectory)
{
af.push_back(strConv);
}
else //if common directory , recursively finding
{
memset(tempPath,0,MAX_PATH);
sprintf(tempPath,"%s\\%s",pathName,strConv.c_str());
ListAllFiles(tempPath,af);
}
bFinish = (FindNextFile(FileHandle,&FindFileData) == FALSE);
}//end while
FindClose(FileHandle);
}
//显示
int dispaly_all_filenames(vector<string> vs)
{
vector<string>::iterator it;
for (it = vs.begin(); it != vs.end(); it++)
{
cout<<*it <<endl;
}
return 0;
}
int main(int argc, char* argv[])
{
if (argc != 2)
{
printf("用法: PicnameSort \"路径名\" \n");
printf("比如: PicnameSort d:\\pic\n");
}
else
{
vector<string> vs_filenames;
ListAllFiles(argv[1],vs_filenames);
dispaly_all_filenames(vs_filenames);
printf("排序后:\n\n");
sort(vs_filenames.begin(),vs_filenames.end()); //排序
dispaly_all_filenames(vs_filenames);
}
return 0;
}