本文介绍了我需要使用c ++在另一个目录中创建一个目录(文件夹)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我编写了以下代码,但我的问题是我无法一次创建2个子目录。例如: - 假设这是现有的路径 C:\ MyStuff \ Myzone \Documents和MyStuff是隐藏文件夹 我想在图片文件夹中添加\confidential\work。 我的代码如下I have written the following code but my problem is I am not able to create 2 sub directories at once. for example :- suppose this is the existing path"C:\MyStuff\Myzone\Documents and "MyStuff" is hidden folderand i want to add "\confidential\work" inside the pictures folder.My code is as follows#include "stdafx.h"#include "process.h";#include iostream";#include "windows.h";#include "string";using namespace std;int main(int argc, CHAR* argv[]){ string str1 =C:\MyStuff\Myzone\Documents\\confidential\work" LPCSTR lpMyString =str1.c_str(); // to convert string to LPCSTR CreateDirectory(lpMyString ,NULL); return 0;}// here i am converting string to LPCSTR so as to pass in 'createDirectory' functionkindly let me know how to create both the folders at once using c++推荐答案"C:\\MyStuff\\Myzone\\Documents\\confidential\\work" 2. CreateDirectory 一次只能创建一个目录。所以你必须调用它两次(先用C:\\MyStuff \\Myzone \\Documents\\confidential然后再用C:\\MyStuff \\ Myzone \\\Documents\\confidential\\work)。 3.您必须拥有在上层路径中创建目录的权限。 /> 您应该始终检查 CreateDirectory 的返回值并调用 GetLastError 无法更多地了解失败的原因。 [更新] SHCreateDirectoryEx [ ^ ]函数也可以创建中间目录。所以你可以改用它。但请阅读说明。2. CreateDirectory can only create one directory at a time. So you must call it twice (first with "C:\\MyStuff\\Myzone\\Documents\\confidential" and then with "C:\\MyStuff\\Myzone\\Documents\\confidential\\work").3. You must have the privileges to create directories in the upper path.You should always check the return value of CreateDirectory and call GetLastError upon failures to know more about the reason of the failure.[UPDATE]The SHCreateDirectoryEx[^] function is able to create intermediate directories as well. So you might use this instead. But read the notes.#include "stdafx.h"#include "process.h"#include "iostream"#include "windows.h"#include "string"#include "tchar.h" static class program{public : static BOOL CreateFullDirectory(LPCTSTR dirName, LPSECURITY_ATTRIBUTES lpSA){ TCHAR tmpName[255]; _tcscpy(tmpName, dirName); // Create parent directories for (LPTSTR p = _tcschr(tmpName, _T('/')); p; p = _tcschr(p + 1, _T('/'))) { *p = 0; ::CreateDirectory(tmpName, lpSA); // may or may not already exist *p = _T('/'); } return CreateDirectory(dirName, lpSA);}};int main(){program::CreateFullDirectory("C:/MyStuff/Myzone/Documents/confidential/work", NULL);} 如果你们知道更好的事情,请发表你的答案:)If you guys know anything better then kindly post your answers :) 这篇关于我需要使用c ++在另一个目录中创建一个目录(文件夹)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 14:29