//多字符集 #include "stdafx.h"
#include <Windows.h>
#include <iostream> using namespace std; BOOL MakeSureDirectoryPathExists(char* DirectoryFullPath); int main()
{
char* DirectoryFullPath = "E:\\这个是我们自己创建的多层目录\\DemoSucess\\";//要在最后加上那个双斜杠,否则无法创建最后一层
MakeSureDirectoryPathExists(DirectoryFullPath);
return ;
} BOOL MakeSureDirectoryPathExists(char* DirectoryFullPath)
{
char* Travel = NULL;
char* BufferData = NULL;
DWORD DirectoryAttributes; BufferData = (char*)malloc(sizeof(char)*(strlen(DirectoryFullPath) + )); if (BufferData == NULL)
{
return FALSE;
} strcpy(BufferData, DirectoryFullPath); Travel = BufferData; if (*(Travel + ) == ':')
{
Travel++;
Travel++;
if (*Travel && (*Travel == '\\'))
{
Travel++;
}
}
while (*Travel) //D:\Hello\\World\Shit\0
{
if (*Travel == '\\')
{
*Travel = '\0';
DirectoryAttributes = GetFileAttributes(BufferData); //查看是否是否目录 目录存在吗//这里注意单双字使用多字符集
if (DirectoryAttributes == 0xffffffff)
{
if (!CreateDirectory(BufferData, NULL))
{
if (GetLastError() != ERROR_ALREADY_EXISTS)
{
free(BufferData);
return FALSE;
}
}
}
else
{
if ((DirectoryAttributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
{
free(BufferData);
BufferData = NULL;
return FALSE;
}
} *Travel = '\\';
} Travel = CharNext(Travel);
} cout << "创建多层目录成功!" << endl;
return TRUE;
}