
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

C:\Ubuntu\Shared\PA 1\getoptDemo.c 1
#include
int main(int noofarguments, char**argvalues) { int opt; bool pflag = false, rflag = false, dflag = false, errflag = false;
int p_option, r_option;
// The following switch and getopt statements execute the p, r and d flag processing of data
while((opt = getopt(noofarguments, argvalues, "p:r:d")) != EOF) {
switch(opt) { case 'p': pflag = true;
if(optarg == NULL) { cerr << "INVALID ARGUMENT" << endl; } else{ p_option = atoi(optarg); // Integer after -p is stored as an character cout << "P option argument was " << p_option << endl; } break;
case 'd': dflag = true; // If d flag is set, then set the boolean value of d_flag to true break;
case 'r':
rflag = true;
if (optarg == NULL) { errflag = true; }
else{
r_option = atoi(optarg); // Since there is an integer after -r cout << "R option argument was " << r_option << endl;
}
break;
default: errflag = true; } }
return 0; }