下面给出创建单个文件夹的方法,每一种方法后面都紧跟着对应的删除文件夹的方法。
此处参考博主。
头文件
#include //头文件
#include
using namespace std;
int main()
{
string path = "E:\\1";
bool flag = CreateDirectory(path.c_str(), NULL);
return 0;
}
#include
#include //头文件
using namespace std;
int main()
{
string path = "E:\\1";
bool flag = RemoveDirectory(path.c_str());
return 0;
}
头文件
没有下划线的位不符合ISO c++ 标准的写法,标准要求带下划线的标准,没有下划线的是为了兼容以前的版本。
#include //头文件
#include
using namespace std;
int main()
{
string path = "D:\\1";
mkdir(path.c_str());
return 0;
}
#include //头文件
#include
using namespace std;
int main()
{
string path = "D:\\1";
rmdir(path.c_str());
return 0;
}
#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;
}
头文件:
#include
#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<<"删除成功"<<endl;
}
else
{
cout<<"删除失败"<<endl;
}
return 0;
}
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(
lpcstr lpexistingfilename, //源文件路径
lpcstr lpnewfilename, //新文件路径
bool bfailifexists //为true的话,如果新文件已存在, 则返回false; 为false的话,如果新文件已存在,会将原文件覆盖。
);
函数成功返回true,失败返回false;
CopyFile("C:\\File1.txt","C:\\File2.txt",TRUE);
BOOL MoveFile(
LPCTSTR lpExistingFileName, // file name
LPCTSTR lpNewFileName // new file name
);
MoveFile("C:\\File1.txt","C:\\File3.txt");
#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;
}
#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;
}