Thread: Run a program

  1. #1
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24

    Post Run a program

    is there a way to run a program using C?

    i tried to open an exe file - that just messed it up //good thing it wasnt important

    thanks

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    your question seems vague.
    Are you using any IDEs? (Example: Code::Blocks,Visual Studio,Turbo C,...)
    What compiler are you using?
    How did you try to open the executable file?
    Last edited by stevesmithx; 12-14-2008 at 02:41 PM.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    There are several ways for a C program to call an .exe program.

    There are exec...() functions, spawn...() functions, and the system() function. Maybe others too.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24
    i was looking it up and found:

    system("programname"); // that worked if i put the program in my BIN folder.

    i am using Borland 5.02 compiler.

    when i called the program, another c program i made, it ran in the same dos window the other was currently running in.

    the next thing i wanna know is - how to open this program in a new window dos window

    any ideas?

    Thanks for the replies

  5. #5
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    i am using Borland 5.02 compiler
    You might want to use a new compiler.

    when i called the program, another c program i made, it ran in the same dos window the other was currently running in.

    the next thing i wanna know is - how to open this program in a new window dos window
    In that case you might want to use:
    Code:
    system("start program_name");
    You might also want to use:
    Code:
    system("pause")
    in your called program to prevent the command prompt from closing immediately after execution of the program.

    Anyway,It is NOT recommended to use this method.
    See the faq:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by stevesmithx View Post
    In that case you might want to use:
    Code:
    system("start program_name");
    I remember seeing something like this in another thread, so I don't think Batch commands will work with the system() function. You'd need to do something like this:
    Code:
    system( "cmd.exe /c start program.exe" );
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    UnknownAmateur Programmer
    Join Date
    Sep 2008
    Location
    Were definitly not in Kasas anymore...
    Posts
    25
    I use a 1987 program called Lets C C Compiler. I now use visual studio, but anyway if your running a DOS program on windows if your program exits as soon as you start it right click and go to program on the tabs and unselect close on exit. then try it if that doesnt work I dont know but its worth a try thats how I get my c programs to run. And to launch it use the command CD and then the location, and then enter the name of you program but I normally navigate to the location useing windows explorer and double click the Icon and it opens up in a command prompt.
    Last edited by C-Compiler; 12-15-2008 at 06:19 PM.
    I program using Visual Studio Professional 2008. I have little experience, I'm self taught. I started basic C in early March 2008 with a 1987 compiler and C++ this October with Visual Studio. I normally program in Visual C++ creating form programs. My current project is a simple game where I hide a dot and you have to find it in different random sinerios.

    My Catchphrase:
    DOS wasn't written in a day...

  8. #8
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24
    anyone wants to point me to a better compiler - thanks mucho.

    my sytem is

    4Gig ram
    intel duo.
    Vista Home

    AND THANKS FOR ALL THE HELP AND REPLIES!! XD

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Doc. View Post
    is there a way to run a program using C?

    i tried to open an exe file - that just messed it up //good thing it wasnt important

    thanks
    Try this.

    Code:
    #include <stdlib.h>
     
    int main(){
       system("MyProg.exe");
     
       return 0;
       }

  10. #10
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24
    Code:
    # include <conio.h>
    # include <stdlib.h>
    # include <stdio.h>
    
    void main ()
    {
    	printf ("Press any key to call the program\n\n");
       getch();
       system ("start prgram.bat");
       system("pause");
       printf ("\n\nPress any key to continue");
       getch();
    }
    this is what i tried for the "start program_name" method - cmd reported that they cant find the file.

  11. #11
    Registered User Doc.'s Avatar
    Join Date
    Dec 2008
    Location
    Jamaica
    Posts
    24
    Quote Originally Posted by cpjust View Post
    I remember seeing something like this in another thread, so I don't think Batch commands will work with the system() function. You'd need to do something like this:
    Code:
    system( "cmd.exe /c start program.exe" );


    props to you man!!!

    i got the second dos window opened - but it pops up and goes behind the current one.

    what i am tryin to create is a pop up window effects, like "windows" - u choose an option and a new program, window, runs and when its closed it goes back to the original window.

    Thank for the help guys.

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Never use system()
    Use Win32 api.

  13. #13
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Download GCC, which for windows you find as MinGW. Or download Visual Studio, which is a debugger/IDE/compiler all together.

    You can create a "normal" window, not a shell with CreateWindow() or CreateWindowEX() functions using the win api. Google it.

    But all depends on what you want to do with the new windows. If you want to pop up messages then win api is really what you want. If you indeed want a new shell then system(), spawn() etc etc is a quick solution

  14. #14
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Alex31 View Post
    Never use system()
    Use Win32 api.
    Which would mean that the application won't be portable.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by QuantumPete View Post
    Which would mean that the application won't be portable.

    QuantumPete
    But then again, most of the things you'd do in system() wouldn't be portable anyways, unless you have a bunch of #ifdefs for different platforms that create the string that you pass to system()...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 05:32 PM
  3. Replies: 3
    Last Post: 07-11-2005, 03:07 AM
  4. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM
  5. Cannot run program in background?
    By registering in forum Linux Programming
    Replies: 3
    Last Post: 06-16-2003, 05:47 AM