2011-04-25 3 views
0

기본적으로 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. 

주 (출력) 누락되었습니다 줄 바꿈을이 파일의 끝에 도달하면

답변

1

문제는 마지막 줄을 읽지 않는 것이 아니라, 줄이 버퍼 됨으로 인해 표시하지 않는 것이 문제입니다.

cout << buffer << flush; 

이 작동해야합니다.

+0

이것은 흥미 롭습니다. 프로그램을 종료하면 내 콘솔이 멈춘 것처럼 보입니다. – Spliffster

+0

이상한. 여기 없어. 어떤 터미널 에뮬레이터를 사용하고 있습니까? – AProgrammer

4

FREAD는 EOF를 반환하지 않습니다 출력의 끝에. 오히려, 그것은 0을 반환합니다. 그러나 문제는 apt-get이 입력이 tty가 아니며 프롬프트를 전혀 인쇄하지 않는다는 것을 감지한다는 것입니다.

+0

그리고 읽은 내용이 null로 끝났다고 보장 할 수 없으므로'<< "연산자를 통한 출력은 기껏해야 모호합니다. –

+0

어떤 함수를 사용하여 입력 스트림의 끝까지 읽을 수 있습니까? – Spliffster

+0

+1 fread 문제를 언급했습니다. 디스플레이 문제의 원인은 내 대답을 참조하십시오. – AProgrammer

관련 문제