C++:创建自己的命令

功能:在终端输入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/XXX/模板/CMakeLists.txt %s",argv[1]);
	system(buffer);
	sprintf(buffer,"cp /home/XXX/模板/main.cc %s",argv[1]);
	system(buffer);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值