- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这听起来像是一个愚蠢的问题,我是 C++ 的新手。
在 debian 上,我尝试使用 popen 调用 apt-get install
。我需要解析这个程序的输出。不幸的是,我无法阅读 apt-get 的完整输出。
在某些时候 apt-get 可能会要求用户输入(询问是否应该安装依赖项)。我的程序无法输出这一行。
为什么最后一行(见下面的例子,我的程序中缺少的行是:“Do you want to continue [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);
}
我尝试在 Linux 系统上这样做
$ 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.
最佳答案
尝试将 -y
选项添加到 apt-get
。这将使它假设用户对所有事情都说"is",这应该使它“正常工作”。我认为它现在失败了,因为它想提示用户,但它意识到没有可用的键盘输入(可能通过调用 isatty(3)
)所以它放弃了。
关于C++ apt-get 处理 popen 的标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5781911/
我正在使用java引擎来处理一些内容。 我想知道这样会占用不必要的资源吗? command = 'some command to run my jar file' p = subprocess.Pop
我正在尝试使用以下代码块将一个进程的输出作为输入发送到另一个进程: p1 = Popen(command, stdout=PIPE) p2 = Popen(dgxmcmd, stdin=p1.stdo
popen 缓冲输出而系统没有。这是唯一的区别吗? 我知道 popen 和 system 都通过 shell 运行命令。但是,popen() 是否为 evil作为系统()? 最佳答案 看,从本质上讲,
用代码最容易解释: require 'timeout' puts "this block will properly kill the sleep after a second" IO.popen("
似乎既执行子进程又创建管道进行输入/输出,只是 subprocess 较新。 我的问题是,有没有subprocess.Popen可以做而os.popen不能做的功能,所以我们需要新模块subproce
我有一个生成以下输出的程序: ┌───────────────────────┐ │10 day weather forecast│
我正在使用以下命令来运行 shell 命令(创建子进程): cmd = "ls" process = subprocess.Popen(cmd, shell=True, stdout=subproce
得到结果后,我需要停止通过Python中的Popen发出的服务(在另一个线程的后台运行),但是以下方法失败了(只是使用ping)解释): class sample(threading.Thread):
Python - os.popen 和 subprocess.Popen 有什么区别? 最佳答案 os 进程功能被认为已过时。 subprocess 模块是在 Python 2.4 中引入的,作为与子
根据 python 文档 http://docs.python.org/library/subprocess.html ,建议将 os.popen 替换为 Popen 类,现在我有以下命令: impo
非常具体的问题(我希望):以下三个代码有什么区别? (我希望它只是第一个不等待子进程完成,而第二个和第三个会这样做。但我需要确定这是 only 的区别...) 我也欢迎其他评论/建议(尽管我已经很清楚
我有以下代码: pwd = '/home/user/svnexport/Repo/' updateSVN = "svn up " + pwd cmd = os.popen(updateSVN) get
我正在尝试编写简单的 c 函数,这将使我有可能看到从一个流到另一个流的数据传输进度,并将其显示在我的字符 LCD 上。 我设法传输数据并指示进度,但如何获得管道的结果? 所以基本上我想在 c 中做对应
我正在尝试使用 subprocess 模块与使用 Python 的命令行聊天机器人进行通信。 (http://howie.sourceforge.net/使用编译后的 win32 二进制文件,我有我的
我需要为 Ghostscript 创建一个 Monkey 补丁,我必须从 os.popen 迁移到 subsession.popen 因为我不能在我的系统中使用 shell . 我这样试过: def
在 Linux 操作系统上,下面的 python 代码提供了当前目录中的目录。 dirs = os.popen('ls -d */').read().split(os.linesep) print d
当我们从 Python 2.7.3 升级到 Python 2.7.5 时,大量使用 subprocess.Popen() 的内部库的自动化测试开始失败。该库用于线程环境。调试问题后,我能够创建一个简短
我无法得到它与 bash 相关或 python 子进程,但结果不同: >>> subprocess.Popen("echo $HOME", shell=True, stdout=subprocess.
这里我想执行一个命令,我必须在执行第一个命令后给这个命令输入。 我想执行 obex_test蓝牙模式命令而不是在我必须为启动服务器提供像's'这样的输入之后我怎么能给这个东西。这是我的代码,我写了这个
在我的 Lua 程序中,我必须捕获来自外部程序的输出。这个外部程序需要某些环境变量。所以我这样做: e = "" e = e .. "A=100;" e = e .. "B=Hi;" e = e ..
我是一名优秀的程序员,十分优秀!