1·启动CMD
Windows徽标键 + R 运行窗口 在编辑框输入cmd 点击确定 进入CMD 界面
CMD 命令行包含了WINDOWS API 的许多操作 可以打开文件 设置时间 调整磁盘 这许许多多的功能都离不开一个函数:system(const char* _CMD);
测试后基本上知道CMD 的流程是这样的:
getcwd -> 输入->解析->目录操作?chdir:system->输出->下一条指令
CMD 取工作路径:
getcwd函数
#include<unistd.h>
char *getcwd(char *buf,size_t size);
CMD 设工作路径:
chdir函数
#include<unistd.h>
int *chdir(char *path);
根据以上几个函数,我们就可以写出自己的CMD 了!
#include <iostream>
#include <unistd.h>
using namespace std;
int main(){
char path[64] = {0};
char* buf = NULL;
getcwd(path,sizeof(path));
system("VER"); // 输出当前版本Version
while(true){
cout<<path<<">";
cin>>buf;
if(strcmp(buf,"cd") == 0){
cout<<path<<endl<<endl;
}
else if(strcmp(buf,"cd ..") == 0){
int i = strlen(path - 1)];
while(*pch! = '\\'){
char* pch = &path[i];
i--;
path[i] = 0;
}
}
else if(strcmp(buf,"cd /")){
int i = strlen(path - 1)];
while(*pch! = ':'){
char* pch = &path[i];
i--;
path[i] = 0;
}
}
else
system(buf);
}
return 0;
}