C++中创建文件夹
2015-12-03 11:24
471 查看
最近由于工作的需要,在C++程序中需要创建新文件夹,在网上查阅了一下,现在进行总结一下。
我的程序是运行在windows平台下,所以创建文件夹时需要调用windows里的接口,将程序加上以下几行:
#include “windows.h”
#include "shlwapi.h"
#pragma comment(lib, "shlwapi.lib")
创建文件夹需要两个函数,分别为PathIsDirectoryA和CreateDirectoryA,首先将对它们进行简要介绍:
1.PathIsDirectoryA函数
原型:BOOL PathIsDirectoryA(LPCSTR pszPath);
功能:判断pszPath所指的文件夹是否存在,若存在,返回True,否则返回False;
参数:
pPathName:长指针,指向一个字符串,该字符串指定要判断是否存在的目录的路径。
2.CreateDirectoryA函数
原型:BOOL CreateDirectoryA(LPCSTR lpPathName,
LPSECURITY_ATTRIBUTES lpSecurityAttributes);
功能:创建一个新文件夹;
参数:
pPathName:长指针,指向一个字符串,该字符串指定要创建的目录的路径。
lpSecurityAttributes:忽略,一般设置为NULL。
返回值: 非零表示成功,零表示失败。若想获得更多的错误信息,调用GetLastError函数。
备注: 这个函数不是递归的。它可以在一个路径中创建唯一的最终目录。也就是说,如果父目录或中间目录不存在,该函数将失败并显示错误消息ERROR_PATH_NOT_FOUND。如果要创建多级目录,需要一级一级的创建。
接下来是我在VS2013上写的一个小程序源代码,在程序当前目录下创建文件夹folder1,并在folder1里创建文件夹folder2:
#include "stdafx.h"
#include "iostream"
#include "string"
#include "windows.h.h"
#include "shlwapi.h"
#pragma comment(lib, "shlwapi.lib")
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string strPath = ".\\folder1";
if (!PathIsDirectoryA(strPath.c_str()))
{
if (0 != CreateDirectoryA(strPath.c_str(), NULL))
cout << "Create folder1 successfully!\n";
else
cout << "Fail to create folder1\n";
}
else
cout << "Folder1 already exists!\n";
strPath += "\\folder2";
if (!PathIsDirectoryA(strPath.c_str()))
{
if (0 != CreateDirectoryA(strPath.c_str(), NULL))
cout << "Create folder2 successfully!\n";
else
cout << "Fail to create folder2\n";
}
else
cout << "Folder2 already exists!\n";
cout << endl;
system("pause");
return 0;
}
此外,还可以用函数CreateDirectory和CreateDirectoryW来创建文件夹,但使用时涉及到数据string类型向wstring类型的转换,我尝试了一下,不能将string转化为指针直接复制,所以就放弃了。
注:源代码中包含头文件需要用尖括号,但尖括号在博客中不能显示,故改为了引号!
我的程序是运行在windows平台下,所以创建文件夹时需要调用windows里的接口,将程序加上以下几行:
#include “windows.h”
#include "shlwapi.h"
#pragma comment(lib, "shlwapi.lib")
创建文件夹需要两个函数,分别为PathIsDirectoryA和CreateDirectoryA,首先将对它们进行简要介绍:
1.PathIsDirectoryA函数
原型:BOOL PathIsDirectoryA(LPCSTR pszPath);
功能:判断pszPath所指的文件夹是否存在,若存在,返回True,否则返回False;
参数:
pPathName:长指针,指向一个字符串,该字符串指定要判断是否存在的目录的路径。
2.CreateDirectoryA函数
原型:BOOL CreateDirectoryA(LPCSTR lpPathName,
LPSECURITY_ATTRIBUTES lpSecurityAttributes);
功能:创建一个新文件夹;
参数:
pPathName:长指针,指向一个字符串,该字符串指定要创建的目录的路径。
lpSecurityAttributes:忽略,一般设置为NULL。
返回值: 非零表示成功,零表示失败。若想获得更多的错误信息,调用GetLastError函数。
备注: 这个函数不是递归的。它可以在一个路径中创建唯一的最终目录。也就是说,如果父目录或中间目录不存在,该函数将失败并显示错误消息ERROR_PATH_NOT_FOUND。如果要创建多级目录,需要一级一级的创建。
接下来是我在VS2013上写的一个小程序源代码,在程序当前目录下创建文件夹folder1,并在folder1里创建文件夹folder2:
#include "stdafx.h"
#include "iostream"
#include "string"
#include "windows.h.h"
#include "shlwapi.h"
#pragma comment(lib, "shlwapi.lib")
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string strPath = ".\\folder1";
if (!PathIsDirectoryA(strPath.c_str()))
{
if (0 != CreateDirectoryA(strPath.c_str(), NULL))
cout << "Create folder1 successfully!\n";
else
cout << "Fail to create folder1\n";
}
else
cout << "Folder1 already exists!\n";
strPath += "\\folder2";
if (!PathIsDirectoryA(strPath.c_str()))
{
if (0 != CreateDirectoryA(strPath.c_str(), NULL))
cout << "Create folder2 successfully!\n";
else
cout << "Fail to create folder2\n";
}
else
cout << "Folder2 already exists!\n";
cout << endl;
system("pause");
return 0;
}
此外,还可以用函数CreateDirectory和CreateDirectoryW来创建文件夹,但使用时涉及到数据string类型向wstring类型的转换,我尝试了一下,不能将string转化为指针直接复制,所以就放弃了。
注:源代码中包含头文件需要用尖括号,但尖括号在博客中不能显示,故改为了引号!
相关文章推荐
- UE4 在C++ 动态生成几何、BSP体、Brush ---- Mesh_Generation
- C++之动态链接库和静态链接库归纳…
- c++ list, vector, map, set 区别与用法比较
- KMP算法--c语言实现
- C++ 对文件夹的操作
- c++友元【转载】
- 设计模式C++实现:组合模式
- C++ Error:C2011: 'struct' type redefinition
- 学习c语言的一些简单经验!
- C++ STL学习之queue
- C语言变量名和地址的关系探讨
- Source code for SIFT, ORB, FAST and FFME for OpenCV C++ for egomotion estimation
- C++ 中的问题
- C++栈展开如何防止内存泄露
- C++栈展开如何防止内存泄露
- C++模板:函数模板和模板函数
- C++ vector 和push_back 详解
- c++学习(五)
- 《C语言入门经典》Ivor Horton第10章习题2
- C语言程序设计教程(第三版)课后习题10.3