本人之前有把自己代码保存在某文件夹内的习惯,但是过了不久之后发现了一个问题,在编译文件的时候由于要文件输入输出,经常会有.in,.out文件的产生,以及还有exe文件,占了不少的内存,这么一来这个文件夹里面也不是很美观,以后要搜索这个文件夹内的代码也不是很方便,于是花了一点时间,研究了一下自动删除该文件夹内文件格式不是cpp的文件的程序。
#include<iostream>
#include<fstream>
#include<string>
#include<cstdio>
using namespace std;
int main(){
fstream file_list("name.txt", std::ios::out);
file_list.close();
system("dir /a /b >> name.txt");
file_list.open("name.txt", std::ios::in);
string filename;
string extension;
while(!file_list.eof()){
getline(file_list, filename);
if(filename=="work.exe")continue;
int location=filename.find(".",0);
if(location != string::npos){
extension = filename.substr(location);
if(extension.compare(".cpp")){//如果不是cpp文件,就删掉
const char *savePath=filename.data();
remove(savePath);
}
}
}
file_list.close();
remove("name.txt");
system("PAUSE");
}
使用的时候只需要将这个代码保存在该文件夹内,名字叫做work.cpp,然后编译一遍,每次使用的时候只需要点一下work.exe就可以了(设置了不会删除work.exe)。