Despite the popularity of Windows, many programmers continue to write DOS applications. However, many DOS applications don't provide much of an interface and retrieve runtime parameters or values from the DOS command line. (Some Windows applications accept command-line parameters as well, but this is less common.)
If you've developed command-line applications from the Borland C++ version 3.1 DOS or Windows Integrated Development Environment (IDE), you probably know you can choose Arguments... from the Run menu and then specify the command-line arguments for your program. If you run the program from the IDE, the IDE will pass these arguments to the program.
Unfortunately, specifying your command-line arguments from the
4.0 version of the IDE isn't as straightforward. In this
article, we'll show how you can pass command-line data
to a program from version 4.0 of the Borland C++ IDE.
When you run a program from the IDE, you're actually using the Integrated Debugger, whether you take advantage of its features or not. Accordingly, when you specify a command-line argument for a program you want to run from the IDE, you'll define the argument as one of the Debugger options.
To set the Debugger options, you use the Debugger section of the
Environment Options dialog box. To specify command-line arguments
that you want the IDE to pass to your program, you'll use
the Run Arguments entry field of this dialog box.
Now, let's create a simple program that merely displays its first command-line argument. To begin, launch the Borland C++ 4.0 IDE.
When the IDE's main window appears, choose Close Project from the Project menu (to close any current project) and then choose New from the File menu. In the editor window that appears, enter the code from Listing A.
Listing A: PASS.CPP
#include <iostream.h> int main(int argCount, char** argArray) { if(argCount > 1) { cout << "I don't want "; cout << argArray[1]; cout << ". You take it." << endl; return 0; } else { cout << "Incorrect argument " << endl; cout << "Use: Pass [string]" << endl; return 0; } }
When you finish entering the code, choose Save from the File menu and enter PASS.CPP in the File Name entry field of the Save File As dialog box. Click OK to save the file.
Next, right-click on the editor window for the PASS.CPP source file. Choose Target Expert from the pop-up menu and then select EasyWin[.exe] from the Target Type list box of the TargetExpert dialog box. Click OK to save this change.
To run the program with no command-line argument, choose Run from the Debug menu. After the compiler finishes building and linking the application, you should see the Incorrect Argument Use: Pass [string] error message.
Close the EasyWin window by double-clicking on its System menu icon. Then, in the IDE, choose Environment... from the Options menu. In the Environment Options dialog box, choose Debugger from the Topics list box.
In the Run Arguments entry field, enter THE_BUCK. Now, the Environment Options dialog box should resemble the one shown in Figure A. Click OK to save this setting.
Figure A - You use the Environment Options dialog box to specify command-line arguments.
To run the program with the command-line argument, again choose
Run from the Debug menu. You should see
I don't want THE_BUCK. You take it.
appear in the EasyWin window for the PASS.EXE application. Double-click
on the EasyWin window's System menu icon to close it. Choose
Exit from the File menu to quit the Borland C++ IDE.
If you're developing DOS or Windows applications that accept
command-line arguments, you may want to run those programs from
the IDE (at least at the beginning of the project). By specifying
a command-line argument to the program from the Environment Options
dialog box, you can pass these arguments easily.
Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.