打印本文 打印本文  关闭窗口 关闭窗口  
C++技巧(判断目录是否存在并创建目录)
作者:佚名  文章来源:不详  点击数  更新时间:2008/11/1 20:41:30  文章录入:杜斌  责任编辑:杜斌

  判断目录是否存在并创建目录:
  // 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);
  }
打印本文 打印本文  关闭窗口 关闭窗口