重定向以glib开始的外部应用程序的输出 [英] Redirecting output of an external application started with glib

查看:126
本文介绍了重定向以glib开始的外部应用程序的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用vala通过GLib和spawn_command_line_sync()启动外部应用程序. 根据文档( http://valadoc.org/#!api= glib-2.0/GLib.Process.spawn_sync )中,您可以传递一个字符串来存储外部应用程序的输出.

I'm trying to use vala to start an external application using GLib with spawn_command_line_sync(). According to the documentation (http://valadoc.org/#!api=glib-2.0/GLib.Process.spawn_sync) you can pass a string to store the output of the external application.

虽然在启动打印几行的脚本时这可以正常工作,但我需要调用一个程序来打印二进制文件的内容. (例如"cat/usr/bin/apt-get")

While this works fine when starting a script which prints a couple of lines, I need to call a program which will print the content of a binary file. (for example "cat /usr/bin/apt-get")

有什么方法可以接收外部程序的输出,而不是以字符串形式,而是以DataStream之类的方式接收外部程序的输出吗?

Is there any way how I can receive the output of the external program not in a string, but in a DataStream or something like that ?

我打算将外部程序的输出写入文件,因此仅调用"cat/usr/bin/apt-get> outputfile"将是一种选择(虽然不太好),但它不是似乎有效.

I'm planning to write the ouput of the external program to a file, so just calling "cat /usr/bin/apt-get > outputfile" would be an alternative (not as nice), but it doesn't seem to work.

无论如何,我希望它获得某种输出流. 我将不胜感激.

Anyway I would prefer it to get some kind of Output Stream. I would appreciate any help.

代码即时通讯使用:

using GLib;

static void main(string[] args) {
    string execute = "cat /usr/bin/apt-get";
    string output = "out";

    try {
        GLib.Process.spawn_command_line_sync(execute, out output);
    } catch (SpawnError e) {
        stderr.printf("spawn error!");
        stderr.printf(e.message);
    }

    stdout.printf("Output: %s\n", output);
}

推荐答案

GLib.Process.spawn_async_with_pipes 将使您做到这一点.它产生进程并为stdoutstderrstdin中的每一个返回文件描述符. ValaDoc中有一个代码示例,说明如何设置IOChannel来监视输出.

GLib.Process.spawn_async_with_pipes will let you do just that. It spawns the processes and returns a file descriptor for each of stdout, stderr, and stdin. There's a sample of code in the ValaDoc on how to set up IOChannels to monitor the output.

这篇关于重定向以glib开始的外部应用程序的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆