기본적으로 popen을 사용하여 프로그램을 실행할 때 stdout에서 모든 데이터를 읽길 원합니다. 그러나, 내가 실행하는 프로그램은 내 버퍼에 마지막 출력 줄을 반환하지 않으며 이유를 이해하지 못한다. (내 이해 : 나는 출력을 popd에서 stdout {unbuffered?}로 보낼 것이다).C++ popen이 stdout을 모두 얻지 못한다
// $Id: popen.cpp 126 2011-04-25 18:48:02Z wus $
#include <iostream>
#include <stdio.h>
using namespace std;
/**
* run debians apt-get and check output
*/
int main(int argc, char **argv, char **envp) {
FILE *fp;
char buffer[9];
// select a package which is not installed and has uninstalled dependencies,
// apt-get will ask if it should installed «Y» or nor «n».
char command[255] = "apt-get install python-wxtools";
cout << command << endl;
// Execute command, open /dev/stdout for reading
fp = popen(command, "r");
// read output character by character
while (fread(buffer, 1, 1, fp) != EOF) {
cout << buffer;
}
// close
pclose(fp);
}
기본 출력은 다음과 같습니다
$ sudo apt-get install python-wxtools
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
python-wxgtk2.8 python-wxversion
Suggested packages:
wx2.8-doc wx2.8-examples ruby wish tk8.5 tcsh csh octave3.0 mksh pdksh
python-xml editra
The following NEW packages will be installed:
python-wxgtk2.8 python-wxtools python-wxversion
0 upgraded, 3 newly installed, 0 to remove and 8 not upgraded.
Need to get 5,942kB of archives.
After this operation, 25.0MB of additional disk space will be used.
Do you want to continue [Y/n]?
참고 : 마지막 줄의 마지막에 줄 바꿈.
난 내 자신의 프로그램을 사용
, 마지막 줄$ sudo ./test/popen
g++ -Wall -o test/popen test/popen.cpp
test/popen.cpp: In function ‘int main(int, char**, char**)’:
test/popen.cpp:22: warning: comparison between signed and unsigned integer expressions
apt-get install python-wxtools
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
python-wxgtk2.8 python-wxversion
Suggested packages:
wx2.8-doc wx2.8-examples ruby wish tk8.5 tcsh csh octave3.0 mksh pdksh
python-xml editra
The following NEW packages will be installed:
python-wxgtk2.8 python-wxtools python-wxversion
0 upgraded, 3 newly installed, 0 to remove and 8 not upgraded.
Need to get 5,942kB of archives.
After this operation, 25.0MB of additional disk space will be used.
주 (출력) 누락되었습니다 줄 바꿈을이 파일의 끝에 도달하면
이것은 흥미 롭습니다. 프로그램을 종료하면 내 콘솔이 멈춘 것처럼 보입니다. – Spliffster
이상한. 여기 없어. 어떤 터미널 에뮬레이터를 사용하고 있습니까? – AProgrammer