在C++中,你可以使用第三方库来执行CMD命令
首先,确保已经安装了Boost库。如果没有安装,请访问Boost官网下载并安装。
在你的C++项目中,包含所需的头文件:
#include<iostream>
#include<boost/process.hpp>
boost::process::system()
函数执行CMD命令。例如,要执行ipconfig
命令,你可以这样做:int main() {
try {
int result = boost::process::system("ipconfig");
std::cout << "Command executed with exit code: "<< result<< std::endl;
} catch (const std::exception& e) {
std::cerr << "Error executing command: " << e.what()<< std::endl;
}
return 0;
}
g++ main.cpp -o main -lboost_system
注意:-lboost_system
参数用于链接Boost系统库,这是Boost进程库所需的。
现在,当你运行程序时,它将执行ipconfig
命令并显示结果。你可以根据需要替换为其他CMD命令。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: c++中set容器的功能有哪些