Need Linux system call from C++ to send cout to a string

Posted on 2009-04-17
Last Modified: 2013-11-17
Youi've got some information here, but I can't get it to work. I need to call the system command on a Linux machine, and capture the output that would normally go to the terminal (cout) in a string (or stringstream).
For ex.:
prep string to receive what would normally go to cout
call system("ls -al" );
return cout to normal operation (sent to terminal)
Question by:Zzoni
    1 Comment
    LVL 84

    Accepted Solution

         popen, pclose -- process I/O

         Standard C Library (libc, -lc)

         #include <stdio.h>

         FILE *
         popen(const char *command, const char *type);

         pclose(FILE *stream);

         The popen() function ``opens'' a process by creating a bidirectional pipe
         forking, and invoking the shell.  Any streams opened by previous popen()
         calls in the parent process are closed in the new child process.  Histor-
         ically, popen() was implemented with a unidirectional pipe; hence many
         implementations of popen() only allow the type argument to specify read-
         ing or writing, not both.  Since popen() is now implemented using a bidi-
         rectional pipe, the type argument may request a bidirectional data flow.
         The type argument is a pointer to a null-terminated string which must be
         `r' for reading, `w' for writing, or `r+' for reading and writing.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    KVM vs  Qemu 4 98
    Netbeans Open project initial dir 5 31
    Compiling syslinux 6.0.3 from source on Centos 7 3 128
    Help with Ubuntu 14.04 LTS 10 112
    Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
    Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now