创建自己的命令

功能:在终端输入exe test,创建一个名为test的文件夹,里面有一个build文件夹和从指定目录拷贝过来的main.cc和CMakeLists.txt。

#include <iostream>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sstream>
#include <cstdlib>
#include <stdio.h>
#include <string.h>
using namespace std;

int main(int argc, char **argv){
	if(argc!=2){
		cout<<"Enter a name for the WORKSPACE you want to create."<<endl;
		return 0;
	}
	string dir=argv[1];
	if (access(dir.c_str(),0) == 0){
		cout<<"There has been such a directory named \""<<argv[1]<<"\"."<<endl;
		return 0;
	}
	if(access(dir.c_str(),0) == -1){
		if(mkdir(dir.c_str(),0777)){//rmdir(dir.c_str());
			cout<<"Failed to make directory \""<<dir<<"\"."<<endl;
			return 0;
		}
		stringstream ss;
		ss<<dir<<"/build";
		ss>>dir;
		if(mkdir(dir.c_str(),0777)){
			cout<<"Failed to make directory \"build\"."<<endl;
			return 0;
		}
	}
	char buffer[70];
	sprintf(buffer,"cp /home/boyob/模板/CMakeLists.txt %s",argv[1]);
	system(buffer);
	sprintf(buffer,"cp /home/boyob/模板/main.cc %s",argv[1]);
	system(buffer);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_26697045/article/details/86559628