C++通过命令行方式调用python。
python文件:/home/test/testProjects/run.py
C++代码如下:
- #include
- #include
- #include
- #include
- using namespace std;
-
- int main()
- {
- string finalCmd = "python3 /home/test/testProjects/run.py";
- vector
out_info; - FILE *fp = popen(finalCmd.c_str(), "r");
- if (nullptr == fp){
- cout << "error: popen() failed." << endl;
- }
- char buf[1024]={0};
- while(nullptr != fgets(buf, 1024, fp)){
- out_info.push_back(string(buf));
- cout << string(buf) << endl;
- }
- pclose(fp);
- return 0;
-
- }