首页 > 编程语言 >纯c++删除自身目录,和该目录下的所有内容______以及创建文件夹

纯c++删除自身目录,和该目录下的所有内容______以及创建文件夹

时间:2023-04-23 18:13:45浏览次数:32  
标签:char const int void c++ ______ path include 目录

头文件.h

#ifndef AUTODELETEADDFOLDER_H #define AUTODELETEADDFOLDER_H #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <dirent.h> #include <string.h> #include <iostream> #include <sys/stat.h> #include <sys/types.h> int CreateDir(const char *sPathName); void error_quit( const char *msg ); void change_path( const char *path ); void rm_dir( const char *path ); //格式 path = “/home/wc/qtproject/launchWidget/launchWidget/temp/” //说明 ZDnum :要创建多少个文件夹 void autoDelete_CreateFolder( std::string &path,const int ZDnum); #endif

 

源文件.cpp

#include "autodeleteaddfolder.h" int CreateDir(const char *sPathName) { char DirName[256]; strcpy(DirName, sPathName); int i,len = strlen(DirName); for(i=1; i<len; i++) { if(DirName[i]=='/') { DirName[i] = 0; if(access(DirName, NULL)!=0) { if(mkdir(DirName, 0755)==-1) { printf("mkdir error\n"); return -1; } } DirName[i] = '/'; } } return 0; } void error_quit( const char *msg ) { perror( msg ); exit( -1 ); } void change_path( const char *path ) { if ( chdir( path ) == -1 ) error_quit( "chdir" ); } void rm_dir( const char *path ) { DIR *dir; struct dirent *dirp; struct stat buf; char *p = getcwd( NULL, 0 ); if ( (dir = opendir( path ) ) == NULL ) error_quit( "OpenDir" ); change_path( path ); while ( dirp = readdir( dir ) ) { if ( (strcmp( dirp->d_name, "." ) == 0) || (strcmp( dirp->d_name, ".." ) == 0) ) continue; if ( stat( dirp->d_name, &buf ) == -1)//-1 error_quit( "stat" ); if ( S_ISDIR( buf.st_mode ) ) { rm_dir( dirp->d_name ); continue; } if ( remove( dirp->d_name ) == -1 ) //-1 error_quit( "remove" ); } closedir( dir ); change_path( p ); if ( rmdir( path ) == -1 ) //-1 error_quit( "rmdir" ); } void autoDelete_CreateFolder( std::string &path,const int ZDnum){ rm_dir(path.data()); CreateDir(path.data()); std::string pathYE = path; pathYE.append("YE/"); CreateDir(pathYE.data()); for(int i=0;i<ZDnum;i++) { std::string ZDfolder = path; ZDfolder.append("ZD").append(std::to_string(i)).append("/"); CreateDir(ZDfolder.data()); } }

 

标签:char,const,int,void,c++,______,path,include,目录
From: https://www.cnblogs.com/RedWetPlace/p/17347324.html

相关文章

  • Node.js Event Loop & V8 engine & libuv All In One
    Node.jsEventLoop&V8engine&libuvAllInOne事件循环constcb1=()=>console.log(`1`);constcb2=()=>console.log(`2`);constcb3=()=>console.log(`3`);constcb4=()=>console.log(`4`);constcb5=()=>console.log(......
  • docker的数据管理
    一、如何管理docker容器中的数据管理Docker容器中数据主要有两种方式:数据卷(DataVolumes)和数据卷容器(DataVolumesContainers)。二、数据卷2.1原理将容器内部的配置文件目录,挂载到宿主机指定目录下数据卷默认会一直存在,即使容器被删除宿主机和容器是两个......
  • 「学习笔记」2-SAT问题
    SAT是适定性\(\text{(Satisfiability)}\)问题的简称。一般形式为k-适定性问题,简称k-SAT。而当\(k>2\)时该问题为NP完全的。所以我们只研究\(k=2\)的情况。2-SAT,简单的说就是给出\(n\)个集合,每个集合有两个元素,已知若干个\(<a,b>\),表示\(a\)与\(b\)矛盾(其中......
  • 树的直径,树的中心性质整理
    本文中,设树中所有权都是正的。直径的定义:不经过同一个点两次的最长链。中心的定义:对于点\(u\),如果满足所有点到点\(u\)距离的最大值最小,则点\(u\)是中心。请注意树的中心和树的重心是两个不同的概念。本文中\(u\simv\)代表树上\(u\leftrightsquigarrowv\)唯一路......
  • ai问答:使用vite如何配置多入口页面
    Vite是一个web开发构建工具,它可以用于开发单页应用和多页应用。要在Vite中配置多入口,可以:在vite.config.js中定义多个entry入口:exportdefault{build:{rollupOptions:{input:{main:resolve(__dirname,'index.html'),othe......
  • vue转换js文件 require js
    1define和require方式 不同点define用来定义一个模块的,requireconfig文件配置后才行require 加载模块define来定义模块,还是通过require来加载模块究竟什么时候去使用2TaskExcutionToday文件如何转换compoment方式 ......
  • [CMU15-418] Lecture1 Why Parallelism
    本系列文章为15-418/15-618:ParallelComputerArchitectureandProgramming,Fall2018课程学习笔记课程官网:参考文章:相关资源与介绍:Theme1Theme2Theme3SummaryILP(instructionlevelparallelism)指令级并行不能一直增长,因为一个程序中出现若干不相关指令......
  • 文字反向输出:WinForm实现
    文字反向输出WinForm实现:附件是文字反向输出程序,及源代码;https://files.cnblogs.com/files/yellow3gold/WordReverse.zip......
  • Vue3 customRef
    视频4.customRef作用:创建一个自定义的ref,并对其依赖项跟踪和更新触发进行显式控制。实现防抖效果:<template> <inputtype="text"v-model="keyword"> <h3>{{keyword}}</h3></template><script> import{ref,customRef}from'vue�......
  • 手游(明日方舟)营收与社区动态评论关系分析
    importpandasaspdimportnumpyasnpimportmatplotlibasmpfrompandas.core.algorithmsimportSelectN,diffimportseabornassefrommatplotlibimportpyplotaspltimportwordcloudimportjiebaimportloggingfromPILimportImage##设置中文plt.rcPa......