Find the answer to your Linux question:
Results 1 to 2 of 2
How do i store a systemcommand output in a char variable in C?? This is how i have tried but it dont work... Code: /* C-program for servermaitenence.... */ /* ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer
    Join Date
    Apr 2003
    Location
    Sweden
    Posts
    796

    How do i store command output in a variable in C??


    How do i store a systemcommand output in a char variable in C?? This is how i have tried but it dont work...

    Code:
    /* C-program for servermaitenence.... */
    /* Andutt made 2003                          */
    /*                                                     */
    #include <stdio.h>
    #include <unistd.h>
    #include <linux/unistd.h>
    #include <sys/wait.h>
    #include <sys/stat.h>
    #include <linux/reboot.h>
    
    
    
    main&#40;void&#41; &#123;
            int nr        = 0;
            int max     = 2000;
    
            char* hostname = execl&#40;"/bin/uname","/bin/uname", "-n",  NULL&#41;;
    
            printf&#40;"Running on host&#58;%c", hostname&#41;;
    &#125;
    Regards

    Andutt

  2. #2
    Just Joined!
    Join Date
    Nov 2002
    Location
    USA
    Posts
    99
    Check out the man page for: popen

    Also, check out this URL for example code:
    http://www.linuxhq.com/guides/LPG/node12.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •