Solved

How to start a program from another program ?

Posted on 2004-03-24
9
355 Views
Last Modified: 2010-04-01
I am using C++ in Linux.

Simple: I have a program that prints HELLO in the screen. Now, this program should call another program, let us say a program that in a new window will say THANK YOU FOR HELPING.

I know you can open files and write to files from a program, but I want now to start another simple program from the first program.

Thank you, and if you need more details, et me know it.
0
Comment
Question by:probine
  • 4
  • 3
  • 2
9 Comments
 
LVL 7

Expert Comment

by:leflon
ID: 10665617
Hi probine,

depends a bit on the environment aou are in, but you can use

int system( const char *command );

to start another prog from your app.
if you can give us your env. we can give you other possibilities

hth
leflon
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10665621
If u have the executable of the second file
Then u could use
system ( execuatble path of second file ) ;

However, I doubt if this would give u the second file output in a new window
It would give the output in the same output widow as that of first

Amit
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10665627
Sorry  leflon,

I guess we started the same time

Amit
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:probine
ID: 10665637
This is what I have:

1. A program called main.cpp
2. A program called test.cpp


This is what I have in main:
*************************
include<iostream>
using namespace std;
int main()
{
   cout <<"Hello";
   // I want to execute the program test.cpp here
}

This is what I have in test.cpp
***************************
include<iostream>
using namespace std;
int main()
{
   cout <<"THANK YOU FOR YOUR HELP";
}


Do I have to incude some header file or something ?
0
 
LVL 7

Expert Comment

by:leflon
ID: 10665660
@Amit: yepp happens to me all the time :-)


oops somehow overread the first line  >> I am using C++ in Linux

leflon
0
 
LVL 7

Accepted Solution

by:
leflon earned 50 total points
ID: 10665688
probine,

you compile both files and will get two exe files (say main.exe and test.exe, or main and test, or whatever your compiler settings are).

to use system you have to include stdlib.h in the file where you will use the system() call.

#include<iostream>
#include <stdlib.h>
using namespace std;
int main()
{
   cout <<"Hello";
   system("test.exe"); // if both exe's are in the same path, else provide complete path to test.exe
   // I want to execute the program test.cpp here
}



leflon
0
 

Author Comment

by:probine
ID: 10665719
Great help and easy to understand... thank you leflon and SYS prog.

You both gave me a good answer... Can I split the points ?
0
 

Author Comment

by:probine
ID: 10667522
Perhaps you can help me with this:

I need to print HELLO in the screen every 10 seconds.

What class do I include and how is the code ?
0
 
LVL 7

Expert Comment

by:leflon
ID: 10668776
probine,

do you have to print a certain number of times or till a key is pressed?

you can use time_t (seconds since 1.1.1970) and the according time functions (include <time.h>).

time_t tNow;
time(&tNow);  // getsystem time in seconds now
int ix = 0;
while(ix<10)
{
    cout << "Hello";
    while((tNow+10)>time(NULL))  // loop till actual system time is 10 seconds greater than tNow
    {
// use either
        sleep(1);       //stops thread for 1 second, check your man pages for header needed for this
// or
        usleep(1000);   //stops thread for 1000 microsecond, check your man pages for header needed for this
// you can omit the sleeps, but then the prog will use 100% of the system for the whole time it runs, which is not very good
    }
    time(&tNow);
}

leflon
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now