[Solved] How to create new folders directories from C++?
-
Hey!
I need to create new folders from C++.
I have tried this:@
void masterResource::makeDirectories(QString folder)
{
QStringList fields = folder.split("///");
folder = fields.at(1).trimmed();CreateDirectory("folder+"/Work", NULL); CreateDirectory(folder+"/InTouch", NULL);
}
@But then I get these errors:
error: cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}' for argument '1' to 'WINBOOL CreateDirectoryW(LPCWSTR, LPSECURITY_ATTRIBUTES)'
and
error: cannot convert 'const QString' to 'LPCWSTR {aka const wchar_t*}' for argument '1' to 'WINBOOL CreateDirectoryW(LPCWSTR, LPSECURITY_ATTRIBUTES)'
-
Never mind, got it to work. The function was
@CreateDirectoryA("folder+"/Work", NULL);@
instead of
@CreateDirectory("folder+"/Work", NULL);@
Notice the 'A'.
-
Another option is to use QDir
@
void masterResource::makeDirectories(QString folder)
{
QStringList fields = folder.split("///");
folder = fields.at(1).trimmed();QDir currentDir; currentDir.mkpath(folder + "/Work"); currentDir.mkpath(folder + "/InTouch");
}
@ -
All in all you should do what andreyc says and use portable Qt APIs, but if you won't then:
Don't use A methods of winAPI. They are obsolete ANSI variant and don't handle national characters. You should use the W variants or the "no letter" macros (which get expanded to W variants when UNICODE is defined, which is the default these days). You need to provide a wide string for these, eg.
@
QString path = ...
CreateDirectory(path.toStdWString().c_str());
@
If you use wide character string literals prepend them with letter L, eg. L"some string". That's how you define a wchar_t string literal in c++;Btw. your "folder" variable is a QString and the winAPI function takes a wchar_t pointer (LPCWSTR expands to that) so I don't see how your solution could work.