CFileFind類遍歷查找文件夾下的所有文件,或者指定文件,並使用ofstream輸出到制定文本

void CFindFloderFile::GetFloderFile(CString floderpath)
{
CFileFind finder;
CString filepath;
vector<string> container;
//打開指定文件用於寫數據,若文件不存在則創建它
ofstream out("E:\\QFV_test\\AllFiles.txt");
BOOL bWorking=finder.FindFile(floderpath+"*.*");
while(bWorking)
{
bWorking=finder.FindNextFile();
//我們知道,每個文件夾下都有兩個默認的文件夾,用來指示當前目錄或者上一級目錄,其中"."表示當前目錄,".."表示上一級目錄,遍歷文件夾時要排除這兩個文件夾
if(!finder.IsDots())
{
filepath=finder.GetFilePath();
//判斷當前文件是否是一個文件夾,如果是文件夾則進行迭代
if(finder.IsDirectory())
{
GetFloderFile(filepath);
}
else
{
CString newfilepath=filepath.GetBuffer(0);
//如果成功打開"E:\\QFV_test\\AllFiles.txt",則將數據寫入文件
if(out.is_open())
{
/*
如果想查找制定類型的文件,則使用下面代碼(以".txt"文件爲例)
CString format=filepath.Right(4);
format.MakeLower();
if(format==".txt")
{
out<<newfilepath<<endl;
string strnewfilepath=LPCSTR(newfilepath);
container.push_back(strnewfilepath);
}
*/
out<<newfilepath<<endl;
//由於container.push_back(參數)參數類型要求爲string,所以要將newfilepath類型由CString轉化爲string
//CString轉化爲string兩種轉化方式
//方法1
string strnewfilepath=LPCSTR(newfilepath);
//方法2
//string strnewfilepath=newfilepath.GetBuffer(0);
container.push_back(strnewfilepath);
}
}
}
//cout<<finder.GetFilePath()<<endl;
}
out<<"The file number of this floder is "<<container.size()<<endl;
out.close();
finder.Close();
}
發佈了30 篇原創文章 · 獲贊 34 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章