본문 바로가기

Study/linux

C/C++로 터미널에 명령어 전달하기.

C/C++프로그램으로 터미널을 실행해서 명령어를 전달해보자.

 

`system`함수를 이용한다.

 

코드 예제

 

#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
	//명령어 입력
    cout << "Input command to run!" << endl;
    string command;
    cin >> command;

	//str을 char*로 변환
    const char *c = command.c_str();    

	//터미널에 명령어 전달
    system(c);	
    return 0;
}

 

실행결과

 

참고

https://www.quora.com/How-do-I-use-terminal-commands-in-C++

 

How do I use terminal commands in C++?

Answer (1 of 2): system() in C/C++ system() is used to invoke an operating system command from a C/C++ program. [code] int system(const char *command); [/code]Note: stdlib.h or cstdlib needs to be included to call system. Using system(), we can execute any

www.quora.com