VC里文件操作有很多,本文在這里收錄了一些常見的函數(shù),僅供參考。
1. 判斷一個目錄是否存在
#include"windows.h"
參數(shù): strPath: 目錄的完整路徑,注意不要以'\'結(jié)尾
返回值: 如果為目錄,返回真,否則返回假
BOOLFolderExist(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ù):文件或目錄的完整名字(帶路徑),可以是文件名,也可以是目錄名
返回值: 如果存在,返回真,否則返回假。
BOOL FileExist(CStringstrFileName)
{
CFileFind fFind;
return fFind.FindFile(strFileName);
}
3. 創(chuàng)建一個目錄
BOOLCreateFolder(CString strPath)
{
SECURITY_ATTRIBUTES attrib;
/*
設(shè)置目錄的常見屬性
*/
return ::CreateDirectory(strPath, &attrib);
}
4. 文件大?。?/p>
參數(shù): 文件名字, 注意,如果給的是目錄(文件夾),該函數(shù)返回值不會遞歸計算目錄下所有文件大小。所以該函數(shù)只適 用于文件大小的統(tǒng)計。
返回值: 文件大小。單位為Byte。
DWORDGetFileSize(CString filepath)
{
WIN32_FIND_DATA fileInfo;
HANDLE hFind;
DWORD fileSize;
CString filename;
filename = filepath;
hFind = FindFirstFile(filename,&fileInfo);
if(hFind != INVALID_HANDLE_VALUE)
fileSize = fileInfo.nFileSizeLow;
FindClose(hFind);
return fileSize ;
}
5. 計算文件夾的大小
參數(shù):文件夾的完整路徑。該函數(shù)不使用與文件
返回值: 文件夾的大小,單位為byte。
int64GetFolderSize(CString strDirPath)
{
CString strFilePath;
int64 dwDirSize = 0;
strFilePath += strDirPath;
strFilePath += "\\*.*";
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表示的磁盤空間是相當(dāng)大的,也沒有溢出的可能。
6. 列出某目錄下的所有文件(不遞歸列出)
#include
#include
#include
#include
#include
#include
typedefstd::basic_string _tstring; //寬字符串
typedefstd::list<_tstring> _tslist; //字符串鏈表
/*
返回文件名的鏈表。
filepath 目錄的完整路徑,不帶\\
filefilterlist 文件擴展名列表,可以是多種類型的組合,比如說.txt;.xls;.doc
isordered 是否對文件名排序
*/
_tslistSearchFile(LPCTSTR filepath, LPCTSTR filefilterlist = _T(".*" ), boolisordered = true)
{
assert(filepath !=NULL);
TCHAR buffer[MAX_PATH];
#if _MSC_VER > 1310
/* 1310 for MicrosoftVisual C++ .NET 2003. 1310 represents \version 13 and a 1.0 point release. TheVisual 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_DATAfinddata;
HANDLE searchhandle =INVALID_HANDLE_VALUE;
size_t length=_tcslen(filepath);
if (buffer[length-1] !=_T('\\'))
{
_tcscat_s(buffer,_T("\\*"));// 向字符串結(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 liststring, a example, file filter may be ".txt;.xls;.doc"
_tstring filterstring =filefilterlist;
_tstringfilename(finddata.cFileName);
_tstring::size_typeindex = 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) //如果要求排序,對鏈表進(jìn)行排序
{
filenamelist.sort();//list的排序采用的一般是merge sort
}
return filenamelist;
}
測試代碼:
LPCTSTR s =_T("C:\\temp");
LPCTSTR s1 =_T(".txt; .xls");
_tslist filename =SearchFile(s,s1);
copy( filename.begin(),
filename.end(),
ostream_iterator<_tstring,_tstring::value_type >(wcout, _T("\n") )
);
由于函數(shù)返回的是list,因此有筆不菲的拷貝開銷。我也不確定RVO(返回值)是否會被執(zhí)行,所以如果list很大很大的話,這確實是很糟糕的。解決方法是把list作為引用參數(shù)傳進(jìn)去。這樣就省了一次拷貝的開銷。
voidSearchFile(_tslist& list, LPCTSTR filepath, LPCTSTR filefilterlist =_T(".*" ), bool isordered = true)
以上代碼均通過visual studio 2008編譯,測試運行。