C++ apt-get working on stdout of popen
This might sound like a stupid question, I am new to C++.
On debian I try to invoke apt-get install
with popen. I need to parse the output of this programm. Unfortunately I am not able to read the full output of apt-get.
At some point apt-get might ask for user input (asking if dependencies should be installed). My programm is not able to output this line.
Whis is the last line (see examples below, the line missing in my program is: "Do you want to continue [Y/n]?") not outputted?
When I run the apt-get command manually, the console output looks like this:
$ 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]?
NOTE: no newline at the end of the last line.
when I use my own program, the last line is missing (output)
$ 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.
NOTE: newline at the end of output
My reference implementation of popen looks like this in c++:
// $Id: popen.cpp 126 2011-04-25 18:48:02Z wus $
#inc开发者_JS百科lude <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];
// must use a package which asks for dependencies
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);
}
I try this to do on a Linux System
$ uname -a
Linux shell1 2.6.35-28-server #50-Ubuntu SMP Fri Mar 18 18:59:25 UTC 2011 x86_64 GNU/Linux
$ gcc --version
gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Try adding the -y
option to apt-get
. This will make it assume the user says Yes to everything, which should make it "just work." I think it's failing now because it wants to prompt the user but it realizes there is no keyboard input available (possibly by calling isatty(3)
) so it gives up.
The problem was not actually input buffering but output buffering of cout. Manual flushes solved the problem:
while (fread(buffer, 1, 1, fp) != EOF) {
cout << buffer << flush;
}
精彩评论