当前位置:编程学习 > C/C++ >>

C++的WIN32怎么建文件夹?

啊拉。我要建一个文件夹。
补充:最好能一次建多层。
		
追问:见楼上我的追问。能一次建个多层的文件夹吗?NULL 带别的参数是什么效果?
答案:
调用Win32 API函数CreateDirectory(),例如在c盘创建一个“123”的文件夹:
CreateDirectory("C:\\123",NULL);
接受2个参数,第一个为存在的路径,第2个一般为NULL。
需要头文件Winbace.h,需要链接库Coredll.lib。
下面是msdn的原文:
  BOOL CreateDirectory(
  LPCTSTR lpPathName,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes
  );
  Parameters
  lpPathName
  [in] Long pointer to a null-terminated string that specifies the pathof the directory to be created.
  There is a default string size limit for paths of MAX_PATH characters. Thislimit is related to how the CreateDirectory function parses paths.
  lpSecurityAttributes
  [in] Ignored; set to NULL.
  Return Values
  Nonzero indicates success. Zero indicates failure. To get extended error information, call GetLastError.
  Remarks
  Some file systems, such as NTFS file system, support compression or encryption for individual files and directories. On volumes formatted for such a file system, a new directory inherits the compression and encryptionattributes of its parent directory.
  Requirements
  OS Versions: Windows CE 1.0 and later.
  Header: Winbase.h.
  Link Library: Coredll.lib.
  See Also
  CeCreateDirectory (RAPI) | CreateFile | RemoveDirectory
包含头文件stdlib.h
用以下代码新建,
system("md c:\1");
在c盘新建一个名为1的文件夹。

使用MakeSureDirectoryPathExists

BOOL MakeSureDirectoryPathExists(PCSTR DirPath);
DirPath [in] : 指向一个以NULL结尾的包含正确的指定的路径名,如果路径名的结尾部分不是文件名而是文件夹,那么要以'\'为结束符

在用MakeSureDirectoryPathExists前,要在Project->Settings...->Link->/Object/library modules中加入imagehlp.lib.

上一个:求解一道C++编程题。。。
下一个:关于二级C++公共基础部分

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,