一般來說,在VC里文件操作有很多,本文在這里收錄了一些常見的函數(shù),分享給大家供大家參考。具體如下:
1. 判斷一個目錄是否存在
//參數(shù): strPath: 目錄的完整路徑,注意不要以'/'結(jié)尾
//返回值: 如果為目錄,返回真,否則返回假
BOOL FolderExist(CString strPath)
{
WIN32_FIND_DATA wfd;
BOOL rValue = FALSE;
HANDLE hFind = FindFirstFile(strPath, &wfd);
if ((hFind!=INVALID_HANDLE_VALUE) &&
(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
{
rValue = TRUE;
}
FindClose(hFind);
return rValue;
}
2. 判斷文件或目錄是否存在
參數(shù):文件或目錄的完整名字(帶路徑),可以是文件名,也可以是目錄名
返回值: 如果存在,返回真,否則返回假。
3. 創(chuàng)建一個目錄
設(shè)置目錄的常見屬性
*/
return ::CreateDirectory(strPath, &attrib);
}
4. 文件大小:
參數(shù): 文件名字, 注意,如果給的是目錄(文件夾),該函數(shù)返回值不會遞歸計算目錄下所有文件大小。所以該函數(shù)只適 用于文件大小的統(tǒng)計。
返回值: 文件大小。單位為Byte。
5. 計算文件夾的大小
參數(shù):文件夾的完整路徑。該函數(shù)不使用與文件
返回值: 文件夾的大小,單位為byte。
CString strFilePath;
int64 dwDirSize = 0;
strFilePath += strDirPath;
strFilePath += "http://*.*";
CFileFind finder;
BOOL bFind = finder.FindFile(strFilePath);
while (bFind)
{
bFind = finder.FindNextFile();
if (!finder.IsDots())
{
CString strTempPath = finder.GetFilePath();
if (!finder.IsDirectory())
{
dwDirSize += finder.GetLength();
}
else
{
dwDirSize += GetDirSize(strTempPath);
}
}
}
finder.Close();
return dwDirSize;
}
由于該函數(shù)涉及到遞歸調(diào)用,因此如果是超大大的文件夾,或者文件夾下的子文件夾特別多,
則很有可能造成堆棧溢出。本人測試過系統(tǒng)目錄D和E,均沒有發(fā)生溢出。因此在一般情況下
可以使用。由于返回值為int64,int64表示的磁盤空間是相當大的,也沒有溢出的可能。
6. 列出某目錄下的所有文件(不遞歸列出)
返回文件名的鏈表。
filepath 目錄的完整路徑,不帶//
filefilterlist 文件擴展名列表,可以是多種類型的組合,比如說.txt;.xls;.doc
isordered 是否對文件名排序
*/
_tslist SearchFile(LPCTSTR filepath, LPCTSTR filefilterlist = _T(".*" ), bool isordered = true)
{
assert(filepath != NULL);
TCHAR buffer[MAX_PATH];
#if _MSC_VER > 1310
/* 1310 for Microsoft Visual C++ .NET 2003. 1310 represents /version 13 and a 1.0 point release. The Visual C++ 2005 compiler version is 1400, the number.
*/
_tcscpy_s(buffer, filepath); //_tcscpy_s is a micro for strcpy_s and strwcpy_s
#else
_tcscpy(buffer,filepath); //
#endif
_tslist filenamelist; // initial length is 100
WIN32_FIND_DATA finddata;
HANDLE searchhandle = INVALID_HANDLE_VALUE;
size_t length= _tcslen(filepath);
if (buffer[length-1] != _T('//'))
{
_tcscat_s(buffer,_T("http://*")); // 向字符串結(jié)尾添加/*, 用來查找所有文件
}
if ( (searchhandle = ::FindFirstFile(buffer, &finddata)) != INVALID_HANDLE_VALUE )
{
while (::FindNextFile(searchhandle, &finddata) != 0)
{
if ( !(finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) // 為文件
{
if ( !_tcsicmp(filefilterlist, _T(".*"))) // 將所有文件輸出到鏈表
filenamelist.push_back(finddata.cFileName);
else
{
//get file filter list string, a example, file filter may be ".txt;.xls;.doc"
_tstring filterstring = filefilterlist;
_tstring filename(finddata.cFileName);
_tstring::size_type index = filename.find_last_of(_T('.'));
if (index == _tstring::npos) // 文件沒有擴展名,跳過
continue;
else
{
_tstring extname = filename.substr(index+1); //取得文件的擴展名
_tstring::size_type exist;
exist = filterstring.find(extname);
if (exist != _tstring::npos) //判斷文件的擴展名是否在擴展名列表里
filenamelist.push_back(finddata.cFileName);
}
}
}
}
}
::FindClose( searchhandle );
if (isordered) //如果要求排序,對鏈表進行排序
{
filenamelist.sort(); //list的排序采用的一般是merge sort
}
return filenamelist;
}
測試代碼如下:
copy( filename.begin(),
filename.end(),
ostream_iterator<_tstring, _tstring::value_type >(wcout, _T("/n") )
);
由于函數(shù)返回的是list,因此有筆不菲的拷貝開銷。個人也不確定RVO(返回值)是否會被執(zhí)行,所以如果list很大很大的話,這確實是很糟糕的。解決方法是把list作為引用參數(shù)傳進去。這樣就省了一次拷貝的開銷。
以上代碼均通過visual studio 2008編譯,測試運行。
感興趣的朋友可以測試運行一下本文實例以加深理解。希望本文所述對大家的C++程序設(shè)計有所幫助。
|
新聞熱點
疑難解答
圖片精選