VC里文件操作有很多,本文在这里收录了一些常见的函数,仅供参考。 1. 判断一个目录是否存在 #include "windows.h" 参数: strPath: 目录的完整路径,注意不要以'\'结尾 返回值: 如果为目录,返回真,否则返回假 BOOL
FolderExist(CString strPath) 2. 判断文件或目录是否存在 参数:文件或目录的完整名字(带路径),可以是文件名,也可以是目录名 返回值: 如果存在,返回真,否则返回假。 BOOL FileExist(CString
strFileName) 3. 创建一个目录 BOOL
CreateFolder(CString strPath) 设置目录的常见属性 */ 4. 文件大小: 参数: 文件名字, 注意,如果给的是目录(文件夹),该函数返回值不会递归计算目录下所有文件大小。所以该函数只适 用于文件大小的统计。 返回值: 文件大小。单位为Byte。 DWORD
GetFileSize(CString filepath) 5. 计算文件夹的大小 参数:文件夹的完整路径。该函数不使用与文件 返回值: 文件夹的大小,单位为byte。 int64 GetFolderSize(CString strDirPath) { CString strFilePath; int64 dwDirSize = 0; } 由于该函数涉及到递归调用,因此如果是超大大的文件夹,或者文件夹下的子文件夹特别多, 则很有可能造成堆栈溢出。本人测试过系统目录D和E,均没有发生溢出。因此在一般情况下 可以使用。由于返回值为int64,int64表示的磁盘空间是相当大的,也没有溢出的可能。 6. 列出某目录下的所有文件(不递归列出) #include #include #include #include #include #include typedef std::basic_string _tstring; //宽字符串 typedef std::list<_tstring> _tslist; //字符串链表 /* 返回文件名的链表。 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("\\*")); // 向字符串结尾添加\*, 用来查找所有文件 } 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; } 测试代码: 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") ) ); 由于函数返回的是list,因此有笔不菲的拷贝开销。我也不确定RVO(返回值)是否会被执行,所以如果list很大很大的话,这确实是很糟糕的。解决方法是把list作为引用参数传进去。这样就省了一次拷贝的开销。 void SearchFile(_tslist& list, LPCTSTR filepath, LPCTSTR filefilterlist = _T(".*" ), bool isordered = true) 以上代码均通过visual studio 2008编译,测试运行。
|
|
来自: 月之夜 > 《Computer》