분류 전체보기 (193) 썸네일형 리스트형 [C++] system() 함수 사용 결과 / 쉘 명령 (command) 결과 (stdout) 를 문자열 (string) 로 받기 안녕하세요? 마이콜타이순 입니다. 쉘에서 특정 커맨드를 실행해서 가져오려 할 때 쉘스크립트가 아닌 C++ 을 사용해서는 어떻게 할 수 있는지 알아보았습니다. #include #include #include std::string getResultFromCommand(std::string cmd) { std::string result; FILE* stream; const int maxBuffer = 256; // 버퍼의 크기는 적당하게 char buffer[maxBuffer]; cmd.append(" 2>&1"); // 표준에러를 표준출력으로 redirect stream = popen(cmd.c_str(), "r"); // 주어진 command를 shell로 실행하고 파이프 연결 (fd 반환) if (str.. 이전 1 ··· 22 23 24 25 다음