Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to execute a command and get output of command within C++ using POSIX?


1 Answer
Samual Sam

Here we will see how to use the POSIX command through C++. The process is very simple, we have to use the function called system(). Inside this we have to pass string. That string will contain the POSIX command. 

The syntax is like below.

system(“command”)

Example

#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;
}

Output

Print string using echo command
Hello World
Calculate math expression using bc
3.14285714285714285714

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.