system call variable

This is a discussion on system call variable within the C Programming forums, part of the General Programming Boards category; Hello everyone, I was wondering is it possible to use system() and put the results into a variable. For example, ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45

    system call variable

    Hello everyone,
    I was wondering is it possible to use system() and put the results into a variable. For example,
    Code:
    #include <stdio.h>
    
    
    int main (void) 
    { 
    	
    	char l;
    	printf("Your libc version is:\n");
    		l = system("/lib/libc.so.6 | head -n1 | awk '{print $7}'");
    	printf("%s", l);
    }
    The output of this is:
    Your libc version is:
    2.5,
    (null)
    Or do I need to use something else like excel or fork?
    Thanks in advance,
    Brad

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    You want to use popen():

    Code:
    FILE *data;
    char buff[1024];
    
    data = popen("/some/command foo bar", "r");
    /* Read the output */
    while(fread(buff, 1, sizeof(buff), data) > 0)
    {
        /* Do something with the data */
    }
    pclose(data);

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    Thanks that worked perfectly. Just wondering the differences excel,fork,popen,system.When should they used and not used?
    Thanks again.
    Brad

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Just wondering the differences excel,fork,popen,system.
    Of those functions, system() is the only one that is ANSI C -- the others are all POSIX (less portable).

    You'd use fork() to . . . well, fork . . . your program. execl() would be useful for starting programs (much like system()). And popen() is useful when you need the output from the command, as you have seen.

    For more information, check out some man pages. Here's one for popen(): http://www.opengroup.org/onlinepubs/...xsh/popen.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Incidentally, I believe system() returns an int, but you put it in a char. That's not as bad as then trying to read the char as a string. A char is obviously not a char *.

    If the return value of system() wasn't 0 in that particular case, you probably would have been reading something very odd looking instead of a more friendly notice of (null).

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    Thank you both, that answers my question completely.
    Take care,
    Brad

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C: system call
    By Dedalus in forum C Programming
    Replies: 6
    Last Post: 06-22-2009, 09:45 AM
  2. Interrupted System Call
    By Seiki in forum C Programming
    Replies: 2
    Last Post: 01-28-2009, 10:55 PM
  3. Call a funvtion from a variable
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-18-2009, 06:22 AM
  4. nanosleep() -system call does some confusing things
    By jtk in forum Linux Programming
    Replies: 5
    Last Post: 08-30-2007, 04:15 AM
  5. System call to call another C file from Existing C File
    By simly01 in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2002, 01:29 PM

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