【发布时间】:2014-08-17 12:25:09
【问题描述】:
我从文件夹中获取文件名并将名称发送到vector<string>,但是当我打印vector<string> 时,我发现顺序与文件夹中的文件顺序不同。
我的代码如下所示:
#include <windows.h>
#include <iostream>
#include <vector>
using namespace std;
void searchFileInDirectroy( const string& dir, vector<string>& outList );
void searchFileInDirectroy( const string& dir, vector<string>& outList )
{
WIN32_FIND_DATA findData;
HANDLE hHandle;
string filePathName;
string fullPathName;
filePathName = dir;
filePathName += "\\*.*";
hHandle = FindFirstFile( filePathName.c_str(), &findData );
if( INVALID_HANDLE_VALUE == hHandle )
{
cout<<"Error"<<endl;
return ;
}
do
{
if( strcmp(".", findData.cFileName) == 0 || strcmp("..", findData.cFileName) == 0 )
{
continue;
}
fullPathName = dir;
fullPathName += "\\";
fullPathName += findData.cFileName;
if( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
searchFileInDirectroy( fullPathName, outList );
}
else
{
outList.push_back(fullPathName);
}
} while( FindNextFile( hHandle, &findData ) );
FindClose( hHandle );
}
int main()
{
///get filenames from folder;
vector<string> pathList;
searchFileInDirectroy("D:/OpenCV/calculate laef area--cui.ver2.0/source", pathList);
for(unsigned int i=0;i<pathList.size();i++)
{
cout<<pathList[i]<<endl;
}
return 0;
}
结果是这样的:
我真正想要的是顺序是从 1 到 12。
【问题讨论】:
-
文件夹中的文件没有固有的顺序。
-
您必须对向量进行排序,因为 FindFile 使用文件系统的顺序。
-
对于(逻辑)排序,您可能需要msdn.microsoft.com/library/windows/desktop/…
-
@MrTux 感谢您的回复。你能给我举个例子吗?我试过 FindFile 方法,但没有成功。
-
@just_rookie:我仍然不清楚您是否需要自然排序(即您不喜欢“12”在“2”之前的事实)或本机低级文件系统顺序。