Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

POSIX를 사용하여 C++ 내에서 명령을 실행하고 명령 출력을 얻는 방법은 무엇입니까?

<시간/>

여기서는 C++를 통해 POSIX 명령을 사용하는 방법을 살펴보겠습니다. 프로세스는 매우 간단합니다. system()이라는 함수를 사용해야 합니다. 이 안에 string을 전달해야 합니다. 해당 문자열에는 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