Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ APT在POPN上的STDUT上工作 这听起来像是个愚蠢的问题,我对C++是新的。p>_C++_Debian_Popen_Apt Get_Debian Based - Fatal编程技术网

C++ APT在POPN上的STDUT上工作 这听起来像是个愚蠢的问题,我对C++是新的。p>

C++ APT在POPN上的STDUT上工作 这听起来像是个愚蠢的问题,我对C++是新的。p>,c++,debian,popen,apt-get,debian-based,C++,Debian,Popen,Apt Get,Debian Based,在debian上,我尝试调用apt get install with popen。我需要解析这个程序的输出。不幸的是,我无法阅读apt get的完整输出 在某些时候,apt get可能会要求用户输入,询问是否应该安装依赖项。我的程序无法输出这一行 Whis是最后一行请参见下面的示例,我的程序中缺少的行是:是否要继续[Y/n]?没有输出 当我手动运行apt get命令时,控制台输出如下所示: $ sudo apt-get install python-wxtools Reading package

在debian上,我尝试调用apt get install with popen。我需要解析这个程序的输出。不幸的是,我无法阅读apt get的完整输出

在某些时候,apt get可能会要求用户输入,询问是否应该安装依赖项。我的程序无法输出这一行

Whis是最后一行请参见下面的示例,我的程序中缺少的行是:是否要继续[Y/n]?没有输出

当我手动运行apt get命令时,控制台输出如下所示:

$ 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.
注意:输出末尾的换行符

我的popen参考实现在c++中如下所示:

// $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];
    // 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);
}

尝试将-y选项添加到apt get。这将使它假设用户对所有事情都说是,这将使它正常工作。我认为它现在失败了,因为它想提示用户,但它通过调用isatty3意识到可能没有键盘输入,所以它放弃了。

问题实际上不是输入缓冲,而是cout的输出缓冲。手动刷新解决了此问题:

while (fread(buffer, 1, 1, fp) != EOF) {
    cout << buffer << flush;
}

我希望我的程序的用户选择是否要获取额外的依赖项,因此apt get install-y是,不幸的是,对于我来说没有选项-y将自动用«Yes»回答所有问题。谢谢您的回答。所以您希望捕获apt get的输出并为其提供输入。那真的不可能。请参见下面的一些原因:您可以考虑用-S进行干运行,显示用户的输出,提示它们,然后再与Y一起运行,如果他们同意的话…你也可以考虑使用C++以外的语言来处理这个问题。兹维克:我可以把Y或N发送回APT get。返回工作,这不是我的问题。但我看不懂最后一行,这似乎是读行的esnt。干跑步听起来像是一项工作,谢谢。但我也想找到一种方法来阅读最后一行,我担心的是最后一行不是由apt get发出的,因为它识别出没有用户。如果您运行apt-get-under-strace,您可能会看到它是否打印了该行,或者它是否调用isatty(可能带有参数0)。如果它确实使用isatty来决定是否打印提示,那么让所有这些都起作用的一种方法是在程序中创建的伪终端中运行它。然而,这是一门相当黑暗的艺术,很可能是一项实质性的工程。
while (fread(buffer, 1, 1, fp) != EOF) {
    cout << buffer << flush;
}