Thread: Can i copy the output of system() to an array?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    Can i copy the output of system() to an array?

    Hi,
    is it possible to write the output of a System() command to an array instead of the standard output?
    i am trying to compute the cksum values of individual lines in a text file and store them into an array.
    what is the best way to do this?
    i tried running a loop to do the following:
    1. reading each line into a file,
    2. computing the checksum of that file,
    3. writing the cksum of this to another file
    4. and reading out of this file.

    this seems to be a rather cumbersome method, also, it doesn't work.
    what is the best way to do this?

    thanks!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    26
    I've understand only the first part of your message:
    is it possible to write the output of a System() command to an array instead of the standard output?
    you can use a pipe&dup2 function to redirect the output of system() to the read-end of the pipe...If this does not say anything, let me know that I'll be more clear

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    8
    thanks for replying.
    i've only just started coding in linux, and i'd be grateful if you could elaborate a little more...
    thanks.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think your four-step process given would be about 95 times simpler and easier for you to do, assuming you yourself have the code for computing a checksum and aren't farming it out to another program. If you are farming it out, then you're pretty much stuck with cumbersome.

    By default all you get is the return value of a system() call, not what was printed out to the screen. If you need that output, you'll have to set up a pipe (type "man pipe" and "man dup2" on your system for the manual).

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    26
    Quote Originally Posted by xenanovich View Post
    thanks for replying.
    i've only just started coding in linux, and i'd be grateful if you could elaborate a little more...
    thanks.
    as soon as i have a bit of time i write you the solution for get the output of system

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    26
    for example, if you want the output of ls command you can use the following code.
    but...first of all: this is a dirty solution: if your program must does other things, you will fork the process and in the child use execl.
    then: in my solution the output (for semplicity) is sent to a file (argv[1]) but you can throw it wherever you want once you put it in a file.

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <sys/wait.h>
    #include <unistd.h>
    
    int main(int argc, const char* argv[]) {
      int fd = open(argv[1],O_CREAT|O_RDWR,S_IRUSR|S_IWUSR);
      dup2(fd,STDOUT_FILENO);
      execlp("ls","ls",0);	
      return 0;
    }
    Last edited by np2k; 05-25-2010 at 08:13 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    popen() is a simple way of running a command and reading (or writing) to it using the standard library calls (fgets etc).

    You even get a FILE* as your handle to the I/O of the program you're running.


    All the fork/exec/pipe/dup stuff is hidden from you.
    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.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Ditto on the value of popen(). The only issue (altho it is the same issue with pipes) is that it redirects stdout, and command output can potentially be to stderr. To get around that, redirect stderr in the command string, eg:

    ls -d 2>&1
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    26
    Quote Originally Posted by Salem View Post
    popen() is a simple way of running a command and reading (or writing) to it using the standard library calls (fgets etc).
    yes, it's an elegant solution

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    8
    thanks salem, np2k, mk27 !
    i used popen. works like a dream!
    @tabstop, i didn't write the code for checksum. i was using the unix cksum for this computation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Replies: 2
    Last Post: 04-04-2007, 06:34 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  5. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM