Thread: Redirecting cmd output to an array?

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    12

    Redirecting cmd output to an array?

    Hello everyone,

    I'm writing a program that runs an execution file from within the program.

    When the execution file runs, some text is output in the command screen, which I would like to be able to capture in an array.

    I am trying to avoid redirecting the command output to a file and then capturing it from there.

    Any help would be very much appreciated! Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Something like
    Code:
    FILE *fp = popen("someCommand","r");
    while ( fgets(buff,sizeof(buff),fp) != NULL ) {
      // store buff in your array
    }
    pclose(fp);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    12
    Thanks Salem!

    Do you know what library the 'popen' function is under? I'm compiling it using LabWindows, and it doesn't seem to recognize that function.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The header file that needs included might be "stdio.h" and the function might be named "_popen" instead of "popen".

    Not all C Compilers have "popen" functions.

    No idea if you need to link with a library; but, you often need to define a macro to use the function.

    Tim S.
    Last edited by stahta01; 06-02-2017 at 06:13 PM. Reason: grammar
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirecting output to a file
    By Hablo in forum C Programming
    Replies: 2
    Last Post: 01-16-2014, 08:15 AM
  2. Redirecting input and output
    By LiteHacker in forum C Programming
    Replies: 4
    Last Post: 09-14-2013, 10:38 PM
  3. Redirecting console output
    By _hannes in forum Windows Programming
    Replies: 3
    Last Post: 11-04-2004, 04:51 AM
  4. help with redirecting std output & errors
    By fergie in forum Linux Programming
    Replies: 3
    Last Post: 04-22-2002, 02:59 PM
  5. redirecting the output of my profiler...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-20-2001, 06:20 PM

Tags for this Thread