C++ CreateDirectory()

关于GetSystemDirectory function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx

 

以下代码参考:http://blog.csdn.net/co_diy/article/details/6831701,不过他好像不是用C++。

IDE: Microsoft Visual Studio Community 2017 15.5.2

操作系统:Windows 7 x64

 1 #include "stdafx.h"    /* IDE自行创建的 */
 2 
 3 #include <windows.h>
 4 
 5 int main(int argc, char **argv)
 6 {
 7     if (CreateDirectory(L"H:\\C++\\NewDirectory", NULL)) {
 8         // If the function succeeds, the return value is nonzero.
 9         printf("Create a new directory succeeds. \n");
10     }
11     else {
12         // If the function fails, the return value is zero. 
13         // To get extended error information, call GetLastError.
14         // Possible errors include the following.
15         switch (GetLastError())
16         {
17         case ERROR_ALREADY_EXISTS:
18             printf("The specified directory already exists!");
19             break;
20         case ERROR_PATH_NOT_FOUND:
21             printf("One or more intermediate directories do not exist!");
22             break;
23         }
24     }
25 
26     getchar();
27 
28     return 0;
29 }

 

GetSystemDirectory()的第一个参数"H:\\C++\\NewDirectory"的前面需要加L,否则IDE会报错(应该和IDE有关)。

如果你的电脑里不存在目录:H:\C++,程序会提示:“One or more intermediate directories do not exist!”。这时候,你需要重新指定在一个存在的目录作为参数。

如果要创建的文件夹已存在(即文件夹NewDirectory),程序会提示:“The specified directory already exists!”,意思是指定的目录早已存在。

转载于:https://www.cnblogs.com/Satu/p/8206777.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值