如何在指定目录下创建文件夹,这个问题可以通过API函数CreateDirectory来解决。
CreateDirectory
功能:
This function creates a new directory. If the underlying file system supports security on files and directories, the function applies a specified security descriptor to the new directory.
(创建一个新的文件夹。如果基本的文件系统在文件或文件夹上支持安全描述,那么该函数将在新建的文件夹上应用指定的安全描述)
原型:
BOOL CreateDirectory(
LPCTSTR lpPathName,
LPSECURITY_ATTRIBUTES lpSecurityAttributes
);
参数:
lpPathName:包含将被创建的文件夹路径的字符串,字符串的长度不超过MAX_PATH。
lpSecurityAttributes:忽略,设为NULL
返回值:
成功则返回非零,失败则返回零。
备注:
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 encryption attributes of its parent directory.
In Windows CE 5.0 and later, full path canonicalization is performed before CreateDirectory processes a path name. As a result, trailing backslashes that may appear in a user-provided path name are ignored.
一些文件系统,像NTFS文件系统,对于个别的文件或目录支持压缩或加密。以卷格式化的文件系统,一个新的目录将继承它父目录的压缩和加密特性。
在Windows CE 5.0或以后的版本中,全路径的规范化将在CreateDirectory处理路径名字前执行。结果是,出现在用户指定的路径名中的反斜线将被忽略。
CreateDirectory和CreateDirectoryA没啥区别,唯一的不同点是针对操作系统的,CreateDirectoryA仅支持ANSI格式,而CreateDirectory则支持ANSI和Unnicode格式.一般只有Windows95/98才是ANSI格式,其它的操作系统ANSI和Unnicode格式都支持~~~~
下面这小段代码是如何在指定目录下创建文件夹的
typedef const string CStr; CStr &_path; _path = "C:\\Users\\Mr Jia\\Desktop\\ 一般物体检测\\"; if(_path.size() == 0) return FALSE; static char buffer[1024]; strcpy(buffer, _S(_path)); for (int i = 0; buffer[i] != 0; i ++) { if (buffer[i] == '\\' || buffer[i] == '/') { buffer[i] = '\0'; //CreateDirectoryA(buffer, 0); CreateDirectory(buffer, 0); buffer[i] = '/'; } }
转自并整理:春暖花开 - C++博客
http://www.cppblog.com/SpringSnow/default.html?page=4