1
利用dos指令把檔案夾下所有檔案名存入指定檔案,再從檔案讀取,存入vector中
string imglist_file = "test_result\\imglist.txt";//存儲檔案名稱清單
string temp_imgname;
string dir_command("dir ");
dir_command += m_strImageSet + "/b > " + imglist_file;//m_strImageSet是我之前建立的變量 裡面存放的是檔案夾路徑
ifstream file(imglist_file.c_str(),ios::in);
system(dir_command.c_str());
vector <string> imglist;
//把圖像名稱都存入vector中
while(file>>temp_imgname)
{
imglist.pushback(temp_imgname);
}
file.close();
2
利用_findfirst、_findnext擷取所有檔案名
string temp_imgname;
long hFile = 0;//檔案句柄
_finddata_t fileinfo;//檔案資訊
vector<string> imglist
if((hFile = _findfirst(m_strImageSet+"\\*",&fileinfo)) != -1)//m_strImageSet是我之前建立的變量 裡面存放的是檔案夾路徑
{
do
{
if((fileinfo.attrib & _A_SUBDIR))
{
continue;//如果是目錄則跳過
}
else
{
imglist.push_back(temp_imgname);
}
}while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
3 利用finder擷取
WIN32_FIND_DATA FindFileData;
int finder = 1;
CString m_strImageSet_t = m_strImageSet +"\\*.*";)//m_strImageSet是我之前建立的變量 裡面存放的是檔案夾路徑
CString m_strImageSet_t1 = m_strImageSet +"\\";
string imgname_temp;
vector <string> imglist;
HANDLE hFind = FindFirstFile(m_strImageSet_t, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle. GetLastError reports %d\n", GetLastError ());
return ;
}
else
{
while(finder)
{
if((_tcscmp(FindFileData.cFileName,_T("."))==0)||(_tcscmp(FindFileData.cFileName,_T(".."))==0))//過濾資料總管中預設兩個檔案夾.和..
{
finder = FindNextFile(hFind,&FindFileData);
continue;
}
else
{
temp_imgname = FindFileData.cFileName;
imglist.pushback(temp_imgname);
}
}
}
FindClose(hFind);