CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2007
    Posts
    2

    run programs from command prompt

    Is there a way to run programs from C++ so that it executes a command in a linux command prompt?

    say I wanted to execute several lines of code in the linux command prompt:

    > myprogram.exe
    > dot -o run.jpg run.dot
    > xemacs file.cc
    > ...etc...

    and I wanted to do these hypothetical commands by running a C++ file:

    > g++ -o runthis.exe runthis.cc
    > runthis.exe

    What is the syntax that I should use to be able to do this? And is there a way?

  2. #2
    Join Date
    Jun 2007
    Location
    Vercelli, Italy
    Posts
    87

    Re: run programs from command prompt

    You can use the system(char *str) function, which executes the string pointed to by str as if it was typed at the command prompt. Be careful using this as it is a potential source of security holes (the classic check that you don't accidentally feed it "rm -f *").

    For what you want to do, though, I'd suggest you to use a shell script.
    I owe Paul McKenzie a pizza.

    I am no expert; but they say I can make concepts easy to understand. That's why newbies questions are mine!!! XD

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: run programs from command prompt

    Best way to implement such applications is using shell scripting or Perl as Sk# suggested. Using C++ is an overkill if you are not targetting anything else.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured