Thread: Program to execute shell commands.

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

    Program to execute shell commands.

    How do i make my program execute shell commands again. Like i dunno, for instance,

    ping www.google.com

    so that it would ping google. Isnt there some way for a prog to pass commands to the shell? I dont want a batch file im not pinging google its just an example.

  2. #2
    Inverse Tangent Studios
    Join Date
    Aug 2004
    Posts
    4
    i've used the system(char*) function before...

    Code:
    #include <process.h>
    
    int main(int argc, char* argv[])
    {
    	system("ping www.google.com");
    	return 0;
    }
    that's one way to do it, i'm sure there are others. if this is a console application, system() will print out the output from the command you executed in your program's output.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Is there a way which you can make the computer yuour ping varable, this was an idea, but the code doesn't quite work, so if any1 can c y, then tell me...

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <process.h>
    #include <string>
    
    using namespace std;
    
    string computer;
    
    int main(int argc, char *argv[])
    {
      cout << "Please enter the name of the computer that you want to ping..." << endl;
      cin >> computer;
      system("ping " computer);
      system("PAUSE");
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Finchie_88
    Is there a way which you can make the computer yuour ping varable, this was an idea, but the code doesn't quite work, so if any1 can c y, then tell me...

    Code:
      system("ping " computer);

    The system() command requires one argument that is [b]char *[/c].

    Here's an example built from yours

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    
    int main(int argc, char *argv[])
    {
      string command;
      
      cout << "Please enter the name of the computer that you want to ping..." 
           << endl;
    
      cin  >> command;
    
      command = "ping " + command;
    
      cout << "Executing \"" <<  command << "\"" << endl;
    
      system(command.c_str());
    
      system("PAUSE");
      return 0;
    }
    Dave

  6. #6
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Now thats interesting, I didn't think to do it like that, and the best thing is that it works...

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Finchie_88
    Now thats interesting, I didn't think to do it like that, and the best thing is that it works...
    That's funny; I thought I got the idea from you. You just didn't make a complete string for the entire command including arguments.

    Regards,

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 04-08-2009, 04:23 PM
  2. Runbox, execute a program.
    By Bill Cosby in forum Linux Programming
    Replies: 5
    Last Post: 10-21-2007, 07:35 AM
  3. Replies: 2
    Last Post: 07-27-2007, 12:48 PM
  4. Execute Program Using VB Script
    By brett2 in forum Windows Programming
    Replies: 0
    Last Post: 08-30-2003, 12:40 PM
  5. Executýng Commands In a Program
    By esler in forum C Programming
    Replies: 2
    Last Post: 09-04-2001, 12:59 AM