I was really hoping someone could help me out here, as I am totally stuck. How do I accept an -f flag for file versus defining test.txt, with what I have already written here?
I'm sorry for the lack of comments. I have been busily working on this, and have slacked where it comes to comments
I can do what I am trying to do using argv[], but am trying to do the same thing using getopts, as I have with the -w option and I cannot figure out how to do it with more than one argument.
int main( int argc, char** argv )
{
// Option values
constchar* d_filename = NULL;
constchar* r_filename = NULL;
constchar* w_filename = NULL;
constchar* f_filename = NULL;
// Collect options
int w;
while ((w = getopt( argc, argv, "d:f:r:w:" )) != -1)
switch (w)
{
case'd': d_filename = optarg; break;
case'f': f_filename = optarg; break;
case'r': r_filename = optarg; break;
case'w': w_filename = optarg; break;
}
// Now check to see that it all makes sense.
// For example, if you only want ONE option to be possible at a time:
switch ((int)!!d_filename + (int)!!f_filename + (int)!!r_filename + (int)!!w_filename)
{
case 0: r_filename = "test.txt"; break; // here's a default if no options were specified
case 1: break; // only one option was specified
default: fooey(); // more than one option was specified
}
// Now act on the data.
// The order and relationship here matters depending on your program's design.
// This example assumes, again, that only ONE option is possible at a time.
if (d_filename) delete_file( d_filename );
elseif (f_filename) test_file( f_filename );
etc