• Aucun résultat trouvé

Arguments to main

Dans le document Library Reference (Page 36-39)

Three parameters (arguments) are passed to main by the Borland C++

startup routine: argc, argv, and env.

• argc, an integer, is the number of command-line arguments passed to main, including the name of the executable itself.

• argv is an array of pointers to strings (char *[]).

• argv[O] is the full path name of the program being run.

• argv[l] points to the first string typed on the operating system command line after the program name.

• argv[2] points to the second string typed after the program name.

• argv[argc-l] points to the last argument passed to main.

• argv[argc] contains NULL.

• env is also an array of pointers to strings. Each element of env[] holds a string of the form ENVVAR=value.

• ENVV AR is the name of an environment variable, such as PATH or COMSPEC.

• value is the value to which ENVV AR is set, such as C: \APPS;C: \ TOOLS; (for PATH) or C:\DOS\COMMAND.COM (for COMSPEC).

Refer to the getenv and putenv entries in Chapter 3, and the environ entry in Chapter 4 for more information.

Examining arguments to main

If you declare any of these parameters, you must declare them exactly in the order given: argc, argv, env. For example, the following are all valid

declarations of main's arguments:

int main ()

int main(int argc) /* legal but very unlikely */

int main(int argc, char * argyl])

int main(int argc, char *, argYll, char * env[])]

The declaration int main (int argc) is legal, but it's very unlikely that you would use argc in your program without also using the elements of argv.

The argument env is also available through the global variable _environ.

For all platforms, argc and argv are also available via the global variables _argc and y.rgv.

Here is an example that demonstrates a simple way of using these arguments passed to main:

/* Program ARGS.C */

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[], char *env[]) { int ii

printf("The value of argc is %d \n\n", argc)i

printf("These are the %d command-line arguments passed to"

" main:\n\n", argc)i for (i = Oi i < argci iff)

printf(" argv[%d]: %s\n", i, argv[i])i

printf("\nThe environment string(s) on this system are:\n\n")i for (i = 0; env[i] != NULL; iff)

printf(" env[%d]: %s\n", i, env[i]);

return 0;

}

Suppose you run ARGS.EXE at the command prompt with the following command line:

C:> args first3rg "arg with blanks" 3 4 "last but one" stop!

Note that you can pass arguments with embedded blanks by surrounding them with quotes, as shown by "argument with blanks" and "last but one"

in this example command line.

The output of ARGS.EXE (assuming that the environment variables are set as shown here) would then be like this:

Wildcard arguments

Wildcard arguments are used only in console-mode applications.

Linking with WILDARGS.OBJ

The value of argc is 7

These are the 7 command-line arguments passed to main:

argv[O]: C:\BC4\ARGS.EXE argv[l]: first_arg argv[2]: arg with blanks argv[3]: 3

argv[ 4] :

argv[5]: last but one argv [6]: stop!

The environment string{s) on this system are env[O]: COMSPEC=C:\COMMAND.COM

env[l]: PROMPT=$p $g

env[2]: PATH=C:\SPRINTiC:\DOSiC:\BC4

The maximum combined length of the command-line arguments passed to main (including the space between adjacent arguments and the program name itself) is 255; this is a Win32 limit.

Command-line arguments containing wildcard characters can be expanded to all the matching file names, much the same way DOS expands wildcards when used with commands like COPY. All you have to do to get wildcard expansion is to link your program with the WILDARGS.OBJ object file, which is included with Borland C++.

Once WILDARGS.OBJ is linked into your progl'am code, you can send wildcard arguments of the type *.* to your main function. The argument will be expanded (in the argv array) to all files matching the wildcard mask.

The maximum size of the argv array varies, depending on the amount of memory available in your heap.

If no matching files are found, the argument is passed unchanged. (That is, a string consisting of the wildcard mask is passed to main.)

Arguments enclosed in quotes (" ... ") are not expanded.

The following commands compile the file ARGS.C and link it with the wildcard expansion module WILDARGS.OBJ, then run the resulting executable file ARGS.EXE:

BCC ARGS.C WILDARGS.OBJ ARGS C:\BC4\INCLUDE\*.H "*.C"

When you run ARGS.EXE, the first argument is expanded to the names of all the *.H files in your Bor~and C++ INCLUDE directory. Note that the

expanded argument strings include the entire path. The argument *.C is not expanded because it is enclosed in quotes.

In the IDE, simply specify a project file (from the project menu) that contains the following lines:

ARGS WILDARGS.OBJ

. . If you prefer the wildcard expansion to be the default, modify your standard CW32?LIB library files to have WILDARGS.OBJ linked automati-cally. To accomplish that, remove SETARGV and INITARGS from the libraries and add WILDARGS. The following commands invoke the Turbo librarian (TLIB) to modify all the standard library files (assuming the current directory contains the standard C and C++ libraries and WILDARGS.OBJ):

tlib CW32 -setargv twildargs tlib CW32MT -setargv twildargs tlib -setargv twildargs

Using -p (Pascal calling conventions)

If you compile your program using Pascal calling conventions (described in the Programmer's Guide, Chapter 2), you must remember to explicitly declare main as a C type. Do this with the _ _ cdecl keyword, like this:

int __ cdecl main(int argc, chart argv[], chart envp[])

Dans le document Library Reference (Page 36-39)