Mail Archives: djgpp/1997/07/01/15:52:34
On 1 Jul 1997, DrkWatr wrote:
> I am having problems using command line arguments in my programs. I am used
> to string handling in Turbo c++, so there is some differences in that
> aspect. But tell me what you see that is wrong with the code below. It is
> supposed to set the frame_skip variable according to the option -f2 if
> present.
> c:\game\source\freedom -f2
> int main(int argc, char *argv[ ]);
Are the semi-colon and missing braces for main() typos for this message
or should the line above be:
int main(int argc, char *argv[])
{
> /* some miscellaneous code here */
> char arg_temp[10];
> strcpy (arg_temp, argv[1]);
> if ((arg_temp[0] == '-') && (arg_temp[1] == 'f'))
> frame_skip = atoi (arg_temp[2]);
This should be:
frame_skip = atoi( &arg_temp[2] );
>
> The above code works in Turbo c, but chokes in DJGPP, with numerous
> warnings and errors. Also to debug it i would use a printf statement like
> the below.
> printf ("%s",arg_temp[1]);
This should be:
printf( "%s", &arg_temp[1] );
BTW: look up the getopt() function in the info docs. It is very
powerful, can save you many hundred lines of code, and gives your
programs a standard POSIX compliant command line interface. You can also
get the GNU getopt() source and compile that to gain long command flags
(like --version_info and --help) and optional flags (ie accepting either
-d or -d filename) and optional command argument rearrangement. AFAIK
GNU getopt is not GLPL protected, though one should check that as I have
not looked in a while.
Art S. Kagel, kagel AT bloomberg DOT com
- Raw text -