提问者:小点点

我需要创建应该从SystemCommand()运行“uname”的请求,并以C++中的字符串形式返回响应[closed]


运行时r=Runtime.getRuntime();

进程p=r.exec(“uname-a”);

P.WAITFOR();

BufferedReader b=新的BufferedReader(新的InputStreamReader(p.getInputStream()));

字符串行=“”;

while((line=B.readline())!=null){

System.out.println(行);

}

b.关闭();

//我找到了这个,但是在Java我需要用C++编写


共1个答案

匿名用户

“system”实用程序可用于从C++程序中运行系统命令。

下面是运行命令的工作演示:

// C++ program to run System command

#include <bits/stdc++.h> 
using namespace std; 
int main () { 
    string command;
    cout << "Enter the command to execute: \n";
    getline (cin, command);
    
    cout << "Output:\n"; 
    system(command.c_str());
    return 0; 
}

输出:

Enter the command to execute: 
uname -a
Output:
Linux Check 4.15.0-1044-gcp #70-Ubuntu SMP Mon Sep 16 12:38:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(创建|systemcommand|运行|uname|请求|并以|c++|中|字符串|形式|返回|响应|closed)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?