Command Line Arguments COMP 1002/1402 Purpose At times a program - - PDF document

command line arguments
SMART_READER_LITE
LIVE PREVIEW

Command Line Arguments COMP 1002/1402 Purpose At times a program - - PDF document

Command Line Arguments COMP 1002/1402 Purpose At times a program needs to know some information during startup Examples: A gaming program needs to know the maximum number of players that will participate A file copy program


slide-1
SLIDE 1

1

Command Line Arguments

COMP 1002/1402

Purpose

  • At times a program needs to know some

information during startup

  • Examples:

– A gaming program needs to know the maximum number of players that will participate – A file copy program needs to know the input file (“from file”) and the output file (“to file”) – A bilingual program needs to know in which language to present the information to the user

slide-2
SLIDE 2

2

Examples

  • Manual pages

– man printf

  • List the files in the dirctory

– ls –l

  • Copy files

– cp infile outfile

  • Compiling using make

– make –f myMakeFile

What is needed

  • Transfer the required input from the

command line: >program arg1 arg2 arg3

  • The operating system, which starts the

program, must transfer the parameters to the program

slide-3
SLIDE 3

3

Transferring arguments

int main(void) { /* local definitions */ /* statements */ } int main(int argc, char *argv[]) { /* local definitions */ /* statements */ }

int main(int argc, char *argv[])

Two arguments are passed into the program

  • argc –

– a counter which “tells” the program how many arguments are passed to the program – an integer

  • argv –

– a list of argument which are passed to the program – an array of pointers to

  • strings. Each string is an

argument

\0 \0 \0 \0 \0 5 argv argc

slide-4
SLIDE 4

4

int main(int argc, char *argv[])

cp in.txt out.c

c p \0 i n . t x t \0

  • u t

. c \0 3 argv argc

Example – printing all the arguments

#include “stdio.h” #include “stdlib.h” #include “string.h” int main(int argc, char **argv) { int i; printf(“The program name is: %s \n”,argv[0]); printf(“The number of arguments: %d \n”,argc); for (i = 1; i < argc; i++) { printf(“argument[%d] = %s \n”, i, argv[i]); } return(0); }

slide-5
SLIDE 5

5

Example – handling missing parameters

  • Assuming that we are coding a program, named copyfiles,

that copies an input file to an output file.

– The program expects two arguments an input file and output file copyfiles in.txt out.c

  • What happens if the user does not provide all the required
  • parameters. The user may enter

– copyfiles – copyfiles in.txt – copyfiles in.txt out.c

#include “stdio.h” #include “stdlib.h” #include “string.h” #define NAME_SIZE 1024 char *getFileName(char *fileRole, char **fileName); void main(int argc, char **argv) { int i; char *inFile = NULL; /* a pointer to the name of the input file */ char *outFile = NULL; /* an pointer to the name of the output file */ switch(argc) { case 3: /* set the output file name */

  • utFile = argv[2];

case 2: /* set the input file name */ inFile = argv[1]; default: break; } if (inFile == NULL) getFileName(“input file”, &inFile); /* obtain input file name */ if (outFile == NULL) getFileName(“output file”, &inFile); /* obtain output file name */ /* other statements */ }

slide-6
SLIDE 6

6

Example – handling missing parameters

char *getFileName(char *fileRole, char **fileName) { char temp[NAME_SIZE]; /* a temporary array */ while (1) { printf(“Enter %s name:”); scanf(“%s”,temp); if (strlen(temp) >=1) break; printf(“file name must contain at least one character \n”); } *fileName = malloc(strlen(temp)+1); strcpy(*fileName, temp); return(*fileName); }