C++ 创建文件夹的四种方式
在开头不得不吐槽一下,我要的是简单明了的创建文件夹的方式,看得那些文章给的都是复杂吧唧的一大坨代码,不仔细看鬼知道写的是啥。因此,为了方便以后自己阅读,这里自己写一下 C++ 创建文件夹的四种方式:
文章目录
使用 system() 调用 dos 命令
使用头文件 direct.h 中的 access 和 mkdir 函数
调用 Windows API 函数
调用 MFC 封装好的接口函数
貌似都是 Windows 的
提前说明:从参数角度上看,其实都应该使用 char*,但是为了方便这里使用的都是 string。在 sof 上找到一个方式把 string 转成 char*,就是调用 string 的函数 c_str()。
文本都是在 E:\database 路径下创建一个叫做 testFolder 的文件夹。给出的文件夹路径的方式是基于我的需要,不是最简单的。
使用 system() 调用 dos 命令
#include <iostream>
using namespace std;
int main()
{
string defaultPath = "E:\\database";
string folderPath = defaultPath + "\\testFolder";
string command;
command = "mkdir -p " + folderPath;
system(command.c_str());
return 0;
}
使用头文件 direct.h 中的 access 和 mkdir 函数
关于 direct.h 我觉得 维基百科 上介绍的不错
#include <direct.h>
#include <iostream>
using namespace std;
int main()
{
string defaultPath = "E:\\database";
string folderPath = defaultPath + "\\testFolder";
if (0 != access(folderPath.c_str(), 0))
{
// if this folder not exist, create a new one.
mkdir(folderPath.c_str()); // 返回 0 表示创建成功,-1 表示失败
//换成 ::_mkdir ::_access 也行,不知道什么意思
}
return 0;
}
调用 Windows API 函数
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
string defaultPath = "E:\\database";
string folderPath = defaultPath + "\\testFolder";
if (!GetFileAttributesA(folderPath.c_str()) & FILE_ATTRIBUTE_DIRECTORY) {
bool flag = CreateDirectory(folderPath.c_str(), NULL);
// flag 为 true 说明创建成功
} else {
cout<<"Directory already exists."<<endl;
}
return 0;
调用 MFC 封装好的接口函数
不推荐此方法,出错的话会有点麻烦。
#include <iostream>
#include <shlwapi.h>
using namespace std;
int main()
{
string defaultPath = "E:\\database";
string folderPath = defaultPath + "\\testFolder";
if (!PathIsDirectory(folderPath.c_str())) // 是否有重名文件夹
{
::CreateDirectory(folderPath.c_str(), 0);
}
return 0;
}
如果你出现了错误 undefined reference to imp__PathIsDirectory @ 4,可以参考 undefined reference to imp PathIsDirectory
下面的方法是基于你详细阅读完上述链接后的前提下给出的
说我在 CodeBlocks 下解决该问题的方法:
第一步:在项目上右击,选择 Build Options
第二步: 在 Linker Settings 里添加 libshlwapi.a 文件
c++删除指定文件夹下的所有文件
void DeleteDirectory(CString strPath)
{
CFileFind tempFind;
TCHAR sTempFileFind[MAX_PATH] = { 0 };
wsprintf(sTempFileFind, _T("%s\\*.*"), strPath);
BOOL IsFinded = tempFind.FindFile(sTempFileFind);
while (IsFinded)
{
IsFinded = tempFind.FindNextFile();
if (!tempFind.IsDots())
{
TCHAR sFoundFileName[200] = { 0 };
_tcscpy(sFoundFileName, tempFind.GetFileName().GetBuffer(200));
if (tempFind.IsDirectory())
{
TCHAR sTempDir[200] = { 0 };
wsprintf(sTempDir, _T("%s\\%s"), strPath, sFoundFileName);
DeleteDirectory(sTempDir); //删除文件夹下的文件
RemoveDirectory(sTempDir); //移除空文件
}
else
{
TCHAR sTempFileName[200] = { 0 };
wsprintf(sTempFileName, _T("%s\\%s"), strPath, sFoundFileName);
DeleteFile(sTempFileName);
}
}
}
tempFind.Close();
}
int main()
{
CString m_path;
m_path = "C:\\Users\\YangJingLong\\Desktop\\staistic\\staistic\\data\\";
DeleteDirectory(m_path);
}
---------------------
原文:https://blog.csdn.net/ss33sss/article/details/92771861
相关文章
- 安装cmake,make install 出错:/lib64/libstdc++.so.6:version ' GLBCXX_3.4.26' not found
- Word处理控件Aspose.Words功能演示:使用 Aspose.Words for C++ 在 Qt 应用程序中创建 Word 文档
- C++实例 MySTLString
- 如何编写 C++ 无序容器的哈希函数
- 05 C++ - 作用域运算符(::)
- 03 C++ - CLion创建程序
- C++中对于类来说头文件(.h)和源文件(.cpp)都应该写些什么 (类的常规创建)
- vs2008 C++创建和调用标准DLL
- 纯C++创建Windows窗体(理解WinMain()与WndProc())
- C++MFC编程笔记day01 MFC介绍、创建MFC程序和重写消息处理
- 【创】C/C++无向图的创建-邻接表表示法& DFS & BFS
- 结合C++和GDAL实现shapefile(shp)文件的创建和写入
- C++ 排列最优解算法思想
- [C/CPP系列知识] Type difference of character literals 和 bool in C and C++
- 《好学的C++程序设计》——2.1 数据分类入驻存储空间听候处理
- 《C++代码设计与重用》——2.5 浅拷贝和深拷贝
- 学习C++可以做什么,可以从事哪些领域的工作
- C/C++中动态链接库的创建和调用
- 知名游戏开发者称 C++ 是一种非常糟糕、可怕的语言(C++不是一门可怕的语言,可怕的是一群没有耐心的程序员来使用C++这门语言)
- 智能驾驶 车牌检测和识别(五)《C++实现车牌检测和识别(可实时车牌识别)》
- C++中动态申请二维数组并释放方法
- Ubuntu下C++窗口用Xlib创建时改变背景颜色