C/C++创建和删除文件夹操作(包含多级)

🏰 必定赢365线路检测 📅 2025-07-08 16:22:37 👤 admin 👁️ 9496 👑 552
C/C++创建和删除文件夹操作(包含多级)

下面给出创建单个文件夹的方法,每一种方法后面都紧跟着对应的删除文件夹的方法。 此处参考博主。

一:调用Windows API函数 CreateDirectory()和 RemoveDirectory(),成功返回0,否则返回非零。

头文件

创建:CreateDirectory()

#include //头文件

#include

using namespace std;

int main()

{

string path = "E:\\1";

bool flag = CreateDirectory(path.c_str(), NULL);

return 0;

}

删除: RemoveDirectory()

#include

#include //头文件

using namespace std;

int main()

{

string path = "E:\\1";

bool flag = RemoveDirectory(path.c_str());

return 0;

}

二. 调用C运行库函数int mkdir()和int rmdir(),返回0表示成功,-1失败

头文件

创建:mkdir()或 ::_mkdir()

没有下划线的位不符合ISO c++ 标准的写法,标准要求带下划线的标准,没有下划线的是为了兼容以前的版本。

#include //头文件

#include

using namespace std;

int main()

{

string path = "D:\\1";

mkdir(path.c_str());

return 0;

}

删除:rmdir()或 ::_rmdir()

#include //头文件

#include

using namespace std;

int main()

{

string path = "D:\\1";

rmdir(path.c_str());

return 0;

}

三.调用system命令md 和 rd

创建:

#include

using namespace std;

int main()

{

system("md D:\\1");

system("pause");//屏幕暂停

return 0;

}

删除:

#include

using namespace std;

int main()

{

system("rd D:\\1");

system("pause");//屏幕暂停

return 0;

}

四:检查文件是否存在

使用access()函数,包含头文件

int access(const char *filenpath, int mode);

amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-1。 这个函数还可以检查其它文件属性: 06 检查读写权限 04 检查读权限 02 检查写权限 01 检查执行权限 00 检查文件的存在性 而这个就算这个文件没有读权限,也可以判断这个文件存在于否 存在返回0,不存在返回-1

#include

#include

#include

using namespace std;

int main()

{

string path = "D:\\test1";

if (access(path.c_str(), 0) == -1)//返回值为-1,表示不存在

{

printf("不存在,创建一个\n");

int i = mkdir(path.c_str());

}

return 0;

}

五:删除文件

C/C++ 删除文件 remove函数

头文件: #include //C #include < cstdio > //C++

函数原型:int remove(const char * filename);

返回结果:如果成功返回 0,失败返回“EOF”( -1)。

#include

#include

using namespace std;

int main()

{

char *savePath = "D:\\test1.txt";

if(remove(savePath)==0)

{

cout<<"删除成功"<

}

else

{

cout<<"删除失败"<

}

return 0;

}

DeleteFile,可以用此参数删除指定文件。

BOOL DeleteFile( LPCSTRlpFileName//要删除的文件名的指针 ); 成功返回非零,失败返回0

int main()

{

CString savePath = "D:\\test1.txt";

SetFileAttributes(savePath , FILE_ATTRIBUTE_NORMAL); //去掉文件的系统和隐藏属性

if(DeleteFile(savePath))

{

cout<<"删除成功"<

}

else

{

cout<<"删除失败"<

}

return 0;

}

CopyFile,可以用此参数拷贝指定文件。

copyfile( lpcstr lpexistingfilename, //源文件路径 lpcstr lpnewfilename, //新文件路径 bool bfailifexists //为true的话,如果新文件已存在, 则返回false; 为false的话,如果新文件已存在,会将原文件覆盖。 );

函数成功返回true,失败返回false;

CopyFile("C:\\File1.txt","C:\\File2.txt",TRUE);

MoveFile,可以用此参数改指定文件的文件名。

BOOL MoveFile( LPCTSTR lpExistingFileName, // file name LPCTSTR lpNewFileName // new file name );

MoveFile("C:\\File1.txt","C:\\File3.txt");

六:createDirectory 多级创建文件夹

#include "windows.h"

#include

#include

#include

using namespace std;

bool createDirectory(std::string folder)

{

std::string folder_builder;

std::string sub;

sub.reserve(folder.size());

for (auto it = folder.begin(); it != folder.end(); ++it) {

//cout << *(folder.end()-1) << endl;

const char c = *it;

sub.push_back(c);

if (c == PATH_DELIMITER || it == folder.end() - 1) {

folder_builder.append(sub);

if (0 != ::_access(folder_builder.c_str(), 0)) {

// this folder not exist

if (0 != ::_mkdir(folder_builder.c_str())) {

// create failed

return false;

}

}

sub.clear();

}

}

return true;

}

七:DeleteDirectory多级删除文件夹

#include "windows.h"

#include

#include

#include

using namespace std;

bool DeleteDirectory(CString DirName)

{

CString PUBPATH;

PUBPATH = DirName;

CFileFind tempFind;

DirName += "\\*.*";

BOOL IsFinded = (BOOL)tempFind.FindFile(DirName);

//cout << IsFinded <

while (IsFinded)

{

IsFinded = (BOOL)tempFind.FindNextFile();

if (!tempFind.IsDots())

{

CString strDirName;

strDirName += PUBPATH;

strDirName += "\\";

strDirName += tempFind.GetFileName();

if (tempFind.IsDirectory())

{

DeleteDirectory(strDirName);

}

else

{

SetFileAttributes(strDirName, FILE_ATTRIBUTE_NORMAL); //去掉文件的系统和隐藏属性

DeleteFile(strDirName);

}

}

}

tempFind.Close();

if (!RemoveDirectory(PUBPATH))

{

return false;

}

return true;

}

八:多级创建文件夹(删除已有的文件夹里所有内容)

只需要将七和八结合起来。

#include "windows.h"

#include

#include

#include

using namespace std;

bool createDirectory(std::string folder)

{

std::string folder_builder;

std::string sub;

sub.reserve(folder.size());

for (auto it = folder.begin(); it != folder.end(); ++it) {

//cout << *(folder.end()-1) << endl;

const char c = *it;

sub.push_back(c);

if (c == PATH_DELIMITER || it == folder.end() - 1) {

folder_builder.append(sub);

if (0 != ::_access(folder_builder.c_str(), 0))

{

// this folder not exist

if (0 != ::_mkdir(folder_builder.c_str())) {

// create failed

return false;

}

}

else

{

if (it == folder.end() - 1)//新的文件夹

{ //先删除里面的内容

if (TRUE != DeleteDirectory(folder_builder.c_str())) {

return false;

}

if (0 != ::_mkdir(folder_builder.c_str())) {

// create failed

return false;

}

}

}

sub.clear();

}

}

return true;

}

皇家推荐

固态硬盘报价
365bet体育线上投注

固态硬盘报价

📅 07-02 👁️ 2197
特种兵薪资待遇
必定赢365线路检测

特种兵薪资待遇

📅 07-01 👁️ 7225
安联老乡骄傲不?过去三届世界杯金靴都在拜仁
必定赢365线路检测

安联老乡骄傲不?过去三届世界杯金靴都在拜仁

📅 07-04 👁️ 6011
狄仁杰沙坨为什么黑化
体育365地址

狄仁杰沙坨为什么黑化

📅 06-30 👁️ 3071
老照片之2002年韩日世界杯
必定赢365线路检测

老照片之2002年韩日世界杯

📅 06-28 👁️ 7153
林钰涓名字的含义
必定赢365线路检测

林钰涓名字的含义

📅 07-08 👁️ 7555