system command output.

This is a discussion on system command output. within the C Programming forums, part of the General Programming Boards category; Hello. This is my first post here and I just started learning C, so please do not be too harsh ...

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

    system command output.

    Hello. This is my first post here and I just started learning C, so please do not be too harsh in responces.

    I was wondering how you can get system() or another function to call a system command and return the output not to STDOUT, but to a variable instead. This is so I may parse the output myself and print it how I wish to the user.

    Thanks to anyone who provides valid info.

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    FILE *foo_output;

    system ("foo.exe > foo_response");

    foo_output = fopen ("foo_response", "rt");

    // something like that?
    hello, internet!

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    What operating system are you using?

    If you are using Linux than the shell is powerful. Write a script.

    If you are using Microsoft and VC++6 than you can use the system(" ..."); call in your program. You can also pass command line arguments to the program.

    Use redirection:

    ls /home/User > file.out
    Last edited by Troll_King; 08-21-2002 at 07:28 AM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Another approach is to use a pipe, if you have those functions available to you (they're non-standard). Be careful with this method though, sometimes the pipe will get stuck if things don't go right. Use with caution!
    Code:
    /* This was written under Borland 5.5 */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        char buf[BUFSIZ];
        FILE *fp;
        
        if ((fp = _popen("ping 127.0.0.1", "r")) == NULL)
        {
            perror("pipe failure");
            return (EXIT_FAILURE);
        }
        
        while (fgets(buf, sizeof(buf), fp))
            printf ("OUTPUT: %s\n", buf);
        
        _pclose(fp);
        
        return (EXIT_SUCCESS);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operating System
    By Houssen in forum C Programming
    Replies: 18
    Last Post: 04-22-2008, 12:51 PM
  2. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 12:04 PM
  3. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 06:56 PM
  4. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  5. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 06:12 AM

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