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 1
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 2
Transferring arguments int main(void) int main(int argc, char *argv[]) { { /* local definitions */ /* local definitions */ /* statements */ /* statements */ } } int main(int argc, char *argv[]) Two arguments are passed \0 into the program • argc – \0 – a counter which “tells” the \0 program how many arguments are passed to the \0 program – an integer \0 • argv – – a list of argument which are argv passed to the program – an array of pointers to strings. Each string is an 5 argument argc 3
int main(int argc, char *argv[]) cp in.txt out.c c p \0 i n . t x t \0 o u t . c \0 argv 3 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); } 4
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 */ outFile = 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 */ } 5
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); } 6
Recommend
More recommend