LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-30-2003, 01:30 AM   #1
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
call other program at run time


How to call other program at run time in c++?
Code:
//hello_call_a.cpp
#include <cstdlib> 
using namespace std;
int main() {
  system("");
}
# ./hello_call_a ./hello_a
Why hello_a isn't executed?

quote chapter 2 of <<thinking in c++>>: "To use the system( ) function, you give it a character array that you would normally type at the operating system command prompt. This can also include command-line arguments, and the character array can be one that you fabricate at run time (instead of just using a static character array as shown above). The command executes and control returns to the program."
Thank you.
 
Old 09-30-2003, 02:39 AM   #2
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 17
You want to call the program named by the command line arg ? How about passing this argument, suitably called argv[1] to the system call ? Please think before posting.
 
Old 09-30-2003, 08:29 PM   #3
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
I want to call the program named by the command line argument.
I wonder could someone give me the source code for this?
I am a newbie.
Thank you.

Last edited by Xiangbuilder; 09-30-2003 at 09:54 PM.
 
Old 10-01-2003, 08:19 AM   #4
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 17
/* plain C. Add namespace directives and change header files for C++ */
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
/*
argv[0] = name of executable (on most OSes)
argv[1..agrc-1] = arguments
argv[argc] = NULL
*/
if(argc < 2)
{
fprintf(STDERR, "No argument given.\n");
exit(1);
}
return system(argv[1]);
}
 
Old 10-01-2003, 09:53 AM   #5
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you for your patience.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make a program run at start up time. TruongAn Linux - Software 22 10-26-2005 06:50 PM
run program automatically at specific time simon_qwl Programming 2 03-20-2005 10:58 AM
Call of Duty doesn't run SiW Linux - Software 0 02-11-2005 04:04 AM
i want to run a script or program at boot time!! FreakboY Linux - Newbie 9 10-12-2003 08:03 AM
How to call other program? Xiangbuilder Programming 11 09-11-2003 09:13 PM


All times are GMT -5. The time now is 11:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration