判断目录是否存在

方法一:
CFileFind m_FileFind;
CString m_sFilePath = "D://Program Files//Adobe//Adobe Flash CS3";
if(!m_FileFind.FindFile(m_sFilePath))  //路径不存在则创建该路径
{
      CreateDirectory(m_sFilePath,NULL);
}

方法二:

CString   strFilePath; 
strFilePath   =   "E://图像//";
strFilePath   +=   m_ID; 
if   (!SetCurrentDirectory(strFilePath))   
{
     CreateDirectory(strFilePath, NULL); 
} 

方法三:

FindFirstFile()/
#define _WIN32_WINNT 0x0400

#include "windows.h"

int
main(int argc, char *argv[])
{
  WIN32_FIND_DATA FindFileData;
  HANDLE hFind;

  printf ("Target file is %s./n", argv[1]);

  hFind = FindFirstFile(argv[1], &FindFileData);

  if (hFind == INVALID_HANDLE_VALUE) {
    printf ("Invalid File Handle. Get Last Error reports %d/n", GetLastError ());
  } else {
    printf ("The first file found is %s/n", FindFileData.cFileName);
    FindClose(hFind);
  }

  return (0);
}

方法四:

FILE *fp = fopen(strPath, "r")

if(fp == NULL)
{
//没有同名文件
}

方法五:

 

  CString   strPath;  
  strPath   =   "c://abc.dat"  
  if(GetFileAttributes(strPath)   ==   0xFFFFFFFF)  
  {  
  AfxMessageBox("文件不存在");  
  return;  
  }  

。。。。。。。。。。

程序如下:  
 

 CString   filePath   =   "C://abc//";  
  DWORD   dwAttr   =   GetFileAttributes(filePath);  
  if(dwAttr==-1||(dwAttr&FILE_ATTRIBUTE_DIRECTORY)==0)//目录不存在  
  {  
  if   (   !CreateDirectory(filePath,   NULL)   )  
  {  
  AfxMessageBox("不能创建目录");  
  return;  
  }  
  }  

方法六:

  WIN32_FIND_DATA   tFile;  
  memset(&tFile,   0,   sizeof(tFile));  
  CString   str   =   "d:/eeagadg/...";  
  if(FindFirstFile(str,   &tFile)   ==   INVALID_HANDLE_..)  
          不存在

方法七:

  int   _access(    
        const   char   *path,    
        int   mode    
  );  

。。。。。。。。

  #include   <io.h>  
  .....  
  char   szPathName[MAX_PATH]   =   "C://ABC";  
  if(_access(szPathName,0)   ==   0)  
        MessageBox(NULL,"该文件夹已经存在","提示",0)  
  else  
    CreateDirectory(szPathName,NULL);



方法八:

int	FileExist(const wchar_t* file)
{
	struct _stat st;
	if (_wstat(file, &st) == 0)
		return 1;
	return 0;
}

我在MFC程序中 用的是方法一:

 CString file;

 CFileFind fileFind;



 file=_T("D://T1//T2");



   if (!fileFind.FindFile(file))
   {
   CreateDirectory(file,NULL);
   }

因为用其他方法时,可能要加额外的头文件 所以选择了这一个

参考资料:

VC判断路径是否存在 - 一路前行 - 博客园

怎样用vc中新建一个文件夹并同时向该文件夹中存储数据_百度知道

vc怎么查找某一路径下指定名称的文件是否存在?_编程_互联网_数据库_天涯问答_天涯社区

http://topic.okbase.net/200704/2007040617/2954104.html

如流,新一代智能工作平台

http://topic.csdn.net/t/20040618/13/3103573.html

http://blog.csdn.net/yuvmen/archive/2009/09/10/4540461.aspx

http://topic.csdn.net/t/20030912/11/2251864.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清水迎朝阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值