
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to execute a command and get output of command within C++ using POSIX?
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
- Related Articles
- How to execute a command and get the output of command within C++ using POSIX?
- How to Save Command Output to a File in Linux?
- How to work with Invoke-Command Scriptblock output?
- How to Get the Path of a Linux Command?
- Execute a Command in Multiple Directories on Linux
- How can I execute JavaScript at the command prompt?
- C# Program to Get and Print the Command Line Arguments Using Environment Class
- How to convert command output to the Hashtable format in PowerShell?
- How to exclude PSComputerName property from Invoke-Command output in PowerShell?
- How to get only the file name using find command on Linux?
- Difference between Write-Output and Write-Host command in PowerShell?
- MongoDB profiler output: What is the “command” operation?
- How to install a windows service using windows command prompt in C#?
- How to exclude the RunSpaceID property from the Invoke-Command output in PowerShell?
- Give the panel command to start a specific tablespace within a DB2 database.

Advertisements