Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

วิธี Execute คำสั่งและรับ Output ของคำสั่งภายใน C++ ด้วย POSIX


ที่นี่เราจะดูวิธีใช้คำสั่ง POSIX ผ่าน C++ กระบวนการง่ายมาก เราต้องใช้ฟังก์ชันชื่อ system() ภายในนี้เราต้องส่ง String ที่มีคำสั่ง POSIX

Syntax เป็นดังนี้

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