system commands

This is a discussion on system commands within the C Programming forums, part of the General Programming Boards category; Looking for information on how to get the output from a system command and put them into a variable. I ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    Unhappy system commands

    Looking for information on how to get the output from a system command and put them into a variable. I am writing a short program, proof of concept to my self, on connecting to a DB2 data source. I am able to connect to the data source. I could crate a bind file and bind it to the DB but I want to try a different route and grap the out put from a system command to list the tables of the DB and keep them in a variable with out them showing on the screen. Any ideas. here is a snippet of my code to try just that. sloppy I know, just trying to get it going.


    void DB2Cmd(void)
    {
    char tables[50];

    strcpy(DB2Command,"db2 list tables");
    tables[0] = system(DB2Command);

    printf ("Name of tables %s ", tables[0]);

    printf("\n\n");
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    There are better ways of doing this, depending on which operating system you are using.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    At present I am working it on a Win 98 platform but I intend to get the most use out of it on a s/390 running linux.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    In linux (and other unix-like) operating systems, you have popen

    Code:
    FILE *fp = popen( "db2 list tables", "r" );
    while ( fgets(buff,sizeof(buff), fp ) != NULL ) {
      // do stuff with line of output from the command
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-30-2009, 01:25 AM
  2. running system commands??
    By killdragon in forum C++ Programming
    Replies: 13
    Last Post: 09-25-2004, 03:49 PM
  3. what can i type into msdn to bring up all the system commands
    By Shadow12345 in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2002, 06:58 PM
  4. Capturing system commands
    By manwhoonlyeats in forum C Programming
    Replies: 2
    Last Post: 12-07-2002, 07:38 PM
  5. System(); commands
    By Ryan in forum C++ Programming
    Replies: 15
    Last Post: 03-15-2002, 07:05 PM

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