로딩
요청 처리 중입니다...

C++ 콘솔 실행 명령어, 다른 cpp 컴파일

 C++ 콘솔 실행 명령어, 다른 cpp 컴파일

cmd창에서만 실행할 수 있는 명령어를 C++ 스크립트에서 실행하는 코드이다. 생각보다 간단하다.

#include #include #include "main.h" std::string exec(const char* cmd) { std::array buffer; std::string result; std::unique_ptr pipe(popen(cmd, "r"), pclose); if (!pipe) { throw std::runtime_error("popen() failed!")

; } while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { result += buffer.data(); } return result; } 위 함수를 이용하면 된다.

함수에 인자로 명령어를 넘겨주고 출력하면 끝이다. 예를들면 아래와 같다. int...

# c # cmd # 명령어 # 컴파일 # 콘솔