• Uncategorized

About c++ : Reading-from-console-output-in-C-duplicate

Question Detail

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.

Question Answer

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) {

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() writes output of command executed to cout

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.