windows下创建目录的常见函数

windows下创建目录的常见函数

 

网上搜的都有小小的问题

 

 

bool fileExist(const char* fileName)
{
	WIN32_FIND_DATA wfd;
	HANDLE hHandle = ::FindFirstFile(fileName,&wfd);
	if (hHandle == INVALID_HANDLE_VALUE)
		return false;
	else{
        FindClose(hHandle);
		return (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0; 
      }

}

bool directoryExist(const char* dir)
{
	WIN32_FIND_DATA wfd;
	HANDLE hHandle = ::FindFirstFile(dir,&wfd);
	if (hHandle == INVALID_HANDLE_VALUE)
		return access(dir,0)==0; // if dir is a drive disk path like c:\,we thought is a directory too.  	
	else{
        FindClose(hHandle);
		return (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
  }
}


bool createDirectory(const char* pathName)
{
	char path[MAX_PATH] = {0};
	const char* pos = pathName;
	while ((pos = strchr(pos, '\\')) != NULL)
	{
		memcpy(path, pathName, pos - pathName + 1);
		pos++;
		if (directoryExist(path))
		{
			continue;
		}
		else
		{
			int ret = _mkdir(path);
			if (ret == -1)
			{
				return false;
			}
		}
	}
	pos = pathName + strlen(pathName)-1;
	if (*pos != '\\')
	{
		return _mkdir(pathName)==0;
	}
	return true;
}



bool createFileWithDirectory(const char* pathName)
{
	if (fileExist(pathName))
		return true;
	int len = strlen(pathName);
	if (len <=0)
		return false;
	
	char strTmpPath[MAX_PATH] = {0};
	strcpy(strTmpPath,pathName);
	char* q = strTmpPath + len -1; 
	for (int i = 0; i < len - 1; i++,q--)
	{
		if (*q == '\\')
		{
			*q = '\0';
			q++;
			break;
		}
	}
	if (strlen(strTmpPath) > 0 && strlen(q) > 0)
	{
		createDirectory(strTmpPath);
		FILE* hFile = fopen(pathName, "w");
		if (hFile)
		{
			fclose(hFile);
			return true;
		}
		else
			return false;
				
	}
	else
	{
		return false;
	}
	
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值