C++ 创建文件夹的四种方式
在開頭不得不吐槽一下,我要的是簡單明了的創(chuàng)建文件夾的方式,看得那些文章給的都是復(fù)雜吧唧的一大坨代碼,不仔細看鬼知道寫的是啥。因此,為了方便以后自己閱讀,這里自己寫一下 C++ 創(chuàng)建文件夾的四種方式:
文章目錄
使用 system() 調(diào)用 dos 命令
使用頭文件 direct.h 中的 access 和 mkdir 函數(shù)
調(diào)用 Windows API 函數(shù)
調(diào)用 MFC 封裝好的接口函數(shù)
貌似都是 Windows 的
?
提前說明:從參數(shù)角度上看,其實都應(yīng)該使用 char*,但是為了方便這里使用的都是 string。在 sof 上找到一個方式把 string 轉(zhuǎn)成 char*,就是調(diào)用 string 的函數(shù) c_str()。
文本都是在 E:\database 路徑下創(chuàng)建一個叫做 testFolder 的文件夾。給出的文件夾路徑的方式是基于我的需要,不是最簡單的。
使用 system() 調(diào)用 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 函數(shù)
關(guān)于 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 表示創(chuàng)建成功,-1 表示失敗
? ? ? ? //換成 ::_mkdir ?::_access 也行,不知道什么意思
? ? }
? ? return 0;
}
調(diào)用 Windows API 函數(shù)
#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 說明創(chuàng)建成功
? ? } else {
? ? ? ? cout<<"Directory already exists."<<endl;
? ? }
? ? return 0;
調(diào)用 MFC 封裝好的接口函數(shù)
不推薦此方法,出錯的話會有點麻煩。
#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;
}
如果你出現(xiàn)了錯誤 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?
?
總結(jié)
以上是生活随笔為你收集整理的C++ 创建文件夹的四种方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 职场新人面试误区:我的技术好,所以你必须
- 下一篇: s3c2440移植MQTT