您的位置:首页 > 其它

windows下创建目录的常见函数

2015-05-07 17:00 176 查看
windows下创建目录的常见函数

网上搜的都有小小的问题

bool fileExist(const char* fileName)
{
WIN32_FIND_DATA wfd;
HANDLE hHandle = ::FindFirstFile(fileName,&wfd);
if (hHandle == INVALID_HANDLE_VALUE)
return false;
else
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
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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: