Question
I need help solving the following function. The language is C. ///////////////////////////////////////////////////////////////////////////////////////////////////////// Strings.h ///////////////////////////////////////////////////////////////////////////////////////////////////////// #ifndef STRINGS_H #define STRINGS_H #include void parse_args(int argc, char *argv[], int
I need help solving the following function. The language is C.
///////////////////////////////////////////////////////////////////////////////////////////////////////// Strings.h ///////////////////////////////////////////////////////////////////////////////////////////////////////// #ifndef STRINGS_H #define STRINGS_H #include
void parse_args(int argc, char *argv[], int *cflag, int *wflag, int *nflag, int *lflag, FILE **outfile, int *name_len);
#endif // STRINGS_H ///////////////////////////////////////////////////////////////////////////////////////////////////////// Challenge.c ///////////////////////////////////////////////////////////////////////////////////////////////////////// #include "strings.h"
#include
//1. Use getopt() to parse command line flags, including some that take argument values
// Process command line arguments for a wc-like program // @param argc: number of command line arguments, including the program name // @param argv: array of pointers to strings, one for each argument // @param cflag: address of a 0/1 integer variable - an output of the function // @param wflag: address of a 0/1 integer variable - an output of the function // @param nflag: address of a 0/1 integer variable - an output of the function // @param lflag: address of a 0/1 integer variable - an output of the function // @param outfile: address of a FILE * variable - an output of the function // @param name_len: address of an int variable - an output of the function void parse_args(int argc, char *argv[], int *cflag, int *wflag, int *nflag, int *lflag, FILE **outfile, int *name_len) { // using the getopt library routine, process the command line // arguments given by argc and argv, looking for these options: // -c, -w, -n, -l: set the corresponding flag's int variable to 1 // -o name: try to open for output a FILE with the given name; if that // fails, print to stderr "Error opening output file: xxx " where // xxx is the name of the file, then exit (not return) // -f nnn: set the name_len's int to the integer value given by nnn; // you may use the atoi library function to do the necessary conversion // Default values: // If none of -c, -w, -n, and -l are given, set cflag, wflag, and lflag // (i.e., set the int variable to 1), and unset lflag (value 0). It is // probably easiest to (1) set all the flags' ints to 0 at the beginning; // (2) set individual flags as they are encountered, also remembering if // any have been set; and (3) check for the default case after the // argument processing loop. // If -o is not given, the default output FILE * is for outfile is stdout. // If -f is not given, the default value for name_len is 20. // For outfile and name_len, it is probably easiest to set their default at // the beginning. That way no later check is required. return; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started