C++技巧(判断目录是否存在并创建目录) |
|
www.nanhushi.com 佚名 不详 |
判断目录是否存在并创建目录: // Test Whether the dir exist CString m_dir; if (m_dir.Right(1) == “\\”) m_dir = m_dir.Left(m_dir.GetLength()-1); if (GetFileAttributes(m_dir) == FILE_ATTRIBUTE_DIRECTORY) return TURE; else CreateAllDirectory(m_dir); // Function CreateAllDirectory // recursive function void CreateAllDirectory(CString Dir) { if (Dir.Right(1) == “\\”) Dir = Dir.Left(Dir.GetLength()-1); if (GetFileAttributes == FILE_ATTRIBUTE_DIRECTORY) return; else if (GetFileAttributes(Dir) != -1) { if (DeleteFile(Dir)) // delete the file with the same name if (CreateDirectory(Dir, NULL)) return; MessageBox(_T(“Can not create directory for captured pictures”), NULL, MB_OK); } int n = Dir.ReverseFind(‘\\’); CreateAllDirectory(Dir.Left(n)); if (!CreateDirectory(Dir, NULL)) MessageBox(_T(“Can not create directory for captured pictures”), NULL, MB_OK); } 考试大提示判断Dir中某个文件是否存在,还可以通过FileFind来实现: BOOL FileExist(CString strFileName) { CFileFind fFind; return fFind.FindFile(strFileName); }
|
|
|
文章录入:杜斌 责任编辑:杜斌 |
|
上一篇文章: C++技巧(warningC4786错误解决方法) 下一篇文章: C++技巧(静/动态链接库使用总结) |
【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |
|
|