概述
下面给出创建单个文件夹的方法,每一种方法后面都紧跟着对应的删除文件夹的方法。
此处参考博主。
一:调用Windows API函数 CreateDirectory()和 RemoveDirectory(),成功返回0,否则返回非零。
头文件<windows.h>
创建:CreateDirectory()
#include <Windows.h> //头文件
#include<iostream>
using namespace std;
int main()
{
string path = "E:\1";
bool flag = CreateDirectory(path.c_str(), NULL);
return 0;
}
删除: RemoveDirectory()
#include <iostream>
#include <Windows.h> //头文件
using namespace std;
int main()
{
string path = "E:\1";
bool flag = RemoveDirectory(path.c_str());
return 0;
}
二. 调用C运行库函数int mkdir()和int rmdir(),返回0表示成功,-1失败
头文件<direct.h>
创建:mkdir()或 ::_mkdir()
没有下划线的位不符合ISO c++ 标准的写法,标准要求带下划线的标准,没有下划线的是为了兼容以前的版本。
#include<direct.h> //头文件
#include<iostream>
using namespace std;
int main()
{
string path = "D:\1";
mkdir(path.c_str());
return 0;
}
删除:rmdir()或 ::_rmdir()
#include<direct.h> //头文件
#include<iostream>
using namespace std;
int main()
{
string path = "D:\1";
rmdir(path.c_str());
return 0;
}
三.调用system命令md 和 rd
创建:
#include<iostream>
using namespace std;
int main()
{
system("md D:\1");
system("pause");//屏幕暂停
return 0;
}
删除:
#include<iostream>
using namespace std;
int main()
{
system("rd D:\1");
system("pause");//屏幕暂停
return 0;
}
四:检查文件是否存在
使用access()函数,包含头文件<io.h>
int access(const char *filenpath, int mode);
amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-1。
这个函数还可以检查其它文件属性:
06 检查读写权限
04 检查读权限
02 检查写权限
01 检查执行权限
00 检查文件的存在性
而这个就算这个文件没有读权限,也可以判断这个文件存在于否
存在返回0,不存在返回-1
#include<direct.h>
#include<io.h>
#include<iostream>
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 <stdio.h> //C
#include < cstdio > //C++
函数原型:int remove(const char * filename);
返回结果:如果成功返回 0,失败返回“EOF”( -1)。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
char *savePath = "D:\test1.txt";
if(remove(savePath)==0)
{
cout<<"删除成功"<<endl;
}
else
{
cout<<"删除失败"<<endl;
}
return 0;
}
DeleteFile,可以用此参数删除指定文件。
BOOL DeleteFile(
LPCSTRlpFileName//要删除的文件名的指针
);
成功返回非零,失败返回0
int main()
{
CString savePath = "D:\test1.txt";
SetFileAttributes(savePath , FILE_ATTRIBUTE_NORMAL); //去掉文件的系统和隐藏属性
if(DeleteFile(savePath))
{
cout<<"删除成功"<<endl;
}
else
{
cout<<"删除失败"<<endl;
}
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 <iostream>
#include <io.h>
#include <direct.h>
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 <iostream>
#include <io.h>
#include <direct.h>
using namespace std;
bool DeleteDirectory(CString DirName)
{
CString PUBPATH;
PUBPATH = DirName;
CFileFind tempFind;
DirName += "\*.*";
BOOL IsFinded = (BOOL)tempFind.FindFile(DirName);
//cout << IsFinded <<endl;
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 <iostream>
#include <io.h>
#include <direct.h>
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;
}
最后
以上就是自由爆米花为你收集整理的C/C++创建和删除文件夹操作(包含多级)的全部内容,希望文章能够帮你解决C/C++创建和删除文件夹操作(包含多级)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复