从C ++的控制台输出中读取 [英] Reading from console output in C++

查看:54
本文介绍了从C ++的控制台输出中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Linux的"C ++"中制作一个软件,该软件可以读取ldd控制台应用程序的控制台输出.我想知道系统的共享文件中是否有任何".so"库,或者是在控制台中纯粹读取此命令输出的另一种方法.这是命令输出的示例:

I am trying to make a software in "C++" for linux that reads the console output of the ldd console application. I would like to know if there is any '.so' library in the shared files of the system or another way of purely read the output of this command in console. Here is an example of the output of the command:

ldd ./echo
    linux-vdso.so.1 =>  (0x00007fffdd8da000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe95daf4000)
    /lib64/ld-linux-x86-64.so.2 (0x000055a6179a6000)

此命令显示具有二进制文件的依赖项和位置的列表.我想将此输出保存到变量或其他内容中,以便以后格式化.

This command print a list of the dependencies and the locations that has a binary file. I want to save this output in a variable or something else for being formatted later.

推荐答案

为此,通常必须使用管道函数 popen().运行我们要从中获取输出的程序./p>

For that, usually one has to run the program we want to get output from with a pipe function: popen().

string data;
FILE * stream;
const int max_buffer = 256;
char buffer[max_buffer];

    stream = popen(cmd.c_str(), "r");
    if (stream) {
        while (!feof(stream)) {
            if (fgets(buffer, max_buffer, stream) != NULL) {
                data.append(buffer);
            }
        }
        pclose(stream);
    }
}

通过这种方式,您可以获取ldd的输出并使用它做任何您想做的事情.

This way you can get the output of ldd and do whatever you like with it.

还有其他问题可能会有用:

There is other question you may find useful:

popen()将已执行命令的输出写入cout

这篇关于从C ++的控制台输出中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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