如何使用POSIX在C ++中执行命令并获取命令输出?
在这里,我们将看到如何通过C++使用POSIX命令。该过程非常简单,我们必须使用名为的函数system()。在其中,我们必须传递字符串。该字符串将包含POSIX命令。
语法如下。
system(“command”)
示例
#include <iostream>
using namespace std;
int main () {
cout << "Print string using echo command" << endl;
system("echo 'Hello World'");
cout << "Calculate math expression using bc" << endl;
system("echo '22 / 7' | bc -l");
return 0;
}输出结果
Print string using echo command Hello World Calculate math expression using bc 3.14285714285714285714