使用Unicode Files Path - C ++创建目录树 [英] Create directory tree with Unicode Files Path - C++
本文介绍了使用Unicode Files Path - C ++创建目录树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何用unicode格式创建一个目录?
这样的东西:
Hi,
How can I create a directory with unicode format?
Something like this:
/*
The exists directory path is: "C:\A\B\"
I would create the folderName path ("C:\A\B\C\D\") if not exists.
*/
wchar_t* folderName = L"C:\A\B\C\D\";
bool result = createFolder(folderName);
...
bool createFolder(wchar_t* _folderName)
{
//Return TRUE, if already exists or create is successful
}
Thx,
Bejglee
Thx,
Bejglee
推荐答案
请参考吹码。
Hi,
please refer to the blow code.
#include "stdafx.h"
#include <windows.h>
void CreateDirectoryWithChecking(const wchar_t* i_LPSTR_FolderPath)
{
::WIN32_FIND_DATA data;
::HANDLE handle = ::FindFirstFile(i_LPSTR_FolderPath, &data);
if (handle == INVALID_HANDLE_VALUE)
::CreateDirectory(i_LPSTR_FolderPath, NULL);
else
FindClose(handle);
}
bool createFolderStrecture(const wchar_t* _folderName)
{
if (!_folderName)return false;
size_t len = 0;
while (_folderName[len] != '\0')++len;
for (size_t index = 0; index < len; index++)
{
if (_folderName[index] =='\\' && index>3)
{
wchar_t curFolderPath[_MAX_PATH] = { 0 };
::memcpy_s(curFolderPath, _MAX_PATH*sizeof(wchar_t), _folderName, (index)*sizeof(wchar_t));
CreateDirectoryWithChecking(curFolderPath);
}
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
wchar_t* folderName = L"F:\\A\\B\\C\\D\\";
bool result = createFolderStrecture(folderName);
return 0;
}
谢谢nvtkrishna!
很棒!
我做了一些更改(参见粗体文字),因为我的构建器没有编译它。
但重点是你的!
你认为这没关系?
问候,
Bejglee
Thanks nvtkrishna!
It is great!
I have made some changes (see bold text), because my builder do not compiled it.
But the point is Yours!
You think this is okay?
Regards,
Bejglee
void CreateDirectoryWithChecking(const wchar_t* i_LPCWSTR_FolderPath)
{
::WIN32_FIND_DATAW data;
::HANDLE handle = ::FindFirstFileW(i_LPCWSTR_FolderPath, &data);
if (handle == INVALID_HANDLE_VALUE)
::CreateDirectoryW(i_LPCWSTR_FolderPath, NULL);
else
FindClose(handle);
}
bool createFolderStrecture(const wchar_t* _folderName)
{
if (!_folderName)return false;
size_t len = 0;
while (_folderName[len] != '\0') ++len;
for (size_t index = 0; index < len; index++)
{
if (_folderName[index] == L'\\' && index > 3)
{
wchar_t curFolderPath[_MAX_PATH];
SecureZeroMemory(curFolderPath, _MAX_PATH * sizeof(wchar_t));
::wmemcpy_s(curFolderPath, _MAX_PATH*sizeof(wchar_t), _folderName, (index)*sizeof(wchar_t));
CreateDirectoryWithChecking(curFolderPath);
}
}
return true;
}
这篇关于使用Unicode Files Path - C ++创建目录树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文