how do I store stdout value from system(); into a char* variable?

This is a discussion on how do I store stdout value from system(); into a char* variable? within the C Programming forums, part of the General Programming Boards category; how do I store stdout value from system(); into a char* variable ? e.g. I have child process that executes ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    1

    how do I store stdout value from system(); into a char* variable?

    how do I store stdout value from system(); into a char* variable ?

    e.g.

    I have child process that executes system(); function , but the output is displayed on server (parent) tty,

    I would like to store the output of system("ls -la");

    into a char* variable for later use / display .



    is this possible ?

    thanks,
    Last edited by everyoneca; 08-03-2005 at 02:17 AM.

  2. #2
    pdc
    pdc is offline
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    You want something like:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #define BUFSIZE 100
    
    int main(void) {
      char buf[BUFSIZE];
    
      FILE * f = popen("ls -al", "r");
      size_t r;
      while((r = fread(buf, sizeof(char), BUFSIZE - 1, f)) > 0) {
        buf[r+1] = '\0';
        printf("%s", buf);
      }
    
      pclose(f);
      return 0;
    }
    Last edited by pdc; 08-03-2005 at 05:15 AM. Reason: Typo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read first line of a file and store in a variable
    By Saeid87 in forum C Programming
    Replies: 5
    Last Post: 06-19-2009, 12:27 PM
  2. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  3. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21