Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- CentOS7
- c3 second
- 1697
- c3 초
- semanage
- 정규식 컴파일
- subporcess path
- c3 축 없애기
- snmp
- python os
- 정규식 활용
- snmp test
- python subprocess
- InfluxDB
- c3 step graph
- grafana dashboard
- 정규식 문자열 출력
- g++ 업데이트
- centos pyhon 설치
- c3 축 가리기
- selinux port 등록
- linux시간으로 변경
- telegraf
- gcc regex
- regex_search
- 백준
- gcc 업데이트
- influxdb 설치
- python popen
- c++ 정규식
Archives
- Today
- Total
리셋 되지 말자
[g++] popen 결과값 사용하기 본문
How to execute a command and get the output of command within C++ using POSIX?
How to execute a command and get the output of command within C++ using POSIX? You can use the popen and pclose functions to pipe to and from processes. The popen() function opens a process by creating a pipe, forking, and invoking the shell. We can use a
www.tutorialspoint.com
그리고 아래는 수정한 코드
#include <iostream>
#include <stdexcept>
#include <stdio.h>
#include <string>
using namespace std;
void exec(string command) {
char buffer[128];
FILE* pipe = popen(command.c_str(), "r");
if (!pipe) {
cout << "popen failed!";
}
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
cout<<buffer;
}
pclose(pipe);
return;
}
int main() {
exec("ls");
}
system도 사용이 가능하지만, system은 외부 실행 결과를 정수값으로 뿐이 출력을 못해줘서,
popen을 사용해야 한다.
'gcc' 카테고리의 다른 글
[g++] 정규식 라이브러리 활용 (0) | 2020.01.28 |
---|---|
[g++] 정규식 문자열 찾기 및 찾은 문자열 출력 (0) | 2020.01.28 |
[g++, gcc] gcc 버전 업데이트(업그레이드) (0) | 2020.01.28 |
[g++] c++과 influxdb 연동해서 사용하기 (0) | 2020.01.23 |
[g++] C++ header file path (0) | 2020.01.23 |
Comments