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

จะรันคำสั่งและรับผลลัพธ์ของคำสั่งภายใน C ++ โดยใช้ POSIX ได้อย่างไร


เราจะมาดูวิธีการใช้คำสั่ง POSIX ผ่าน C++ กระบวนการนี้ง่ายมาก เราต้องใช้ฟังก์ชันที่เรียกว่า 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