• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

More on Argv

I'm trying to write my own clone of the UNIX utility Cut.  Syntax as follows:

cut sourcefile -c1,15

I'm still getting familiar with argv and handling arguments passed to it.  I realize that:
argv[0] = cut
argv[1] = sourcefile
argv[2] = -c1,15

When I'm processing the command line args, how can I check that argv[2] contains the "-c"?
0
tsurai
Asked:
tsurai
  • 3
  • 2
  • 2
  • +2
3 Solutions
 
Infinity08Commented:
   if (!strncmp("-c", argv[2], 2)) {
        /* starts with "-c" */
    }
0
 
tsuraiAuthor Commented:
Correction:

argv[0] = cut
argv[1] = -c1,5
argv[2] = filename

So I'd use:
if( !strcmp("-c", argv[1], 1))
?
0
 
jkrCommented:
That would be more like

char* pFound = strstr(argv[1], "-c");

if (pFound == argv[1]) {

  // when both pointers are identical, "-c" was found at the beginning of 'argv[1]'
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jkrCommented:
PS: See also the function reference for 'strstr()' at bhttp://www.cplusplus.com/reference/clibrary/cstring/strstr.html
0
 
Anthony2000Commented:
You might want to look at using a tool like getopt, see:

http://www.pwilson.net/sample.html
http://www.pwilson.net/getopt.html#man

http://argtable.sourceforge.net/    another one I ran across

http://www.dragon-ware.com/~steve/projects/getopts/   non-GPL license

If the number of options is simple, then this is not necessary (in my opinion). But if you have several options then the tools makes it easier to deal with (again, my opinion).

I learned about these type of tools while learning to write linux programs.
0
 
Infinity08Commented:
>> if( !strcmp("-c", argv[1], 1))

No :

        if (!strncmp("-c", argv[1], 2))

with strncmp instead of strcmp and 2 instead of 1 :

        http://www.cplusplus.com/reference/clibrary/cstring/strncmp.html

Btw, if you need to be able to recognize more command line options, then you probably want to parse it one character at a time :

        char *ptr = argv[1];
        if ('-' == *ptr) {
            /* command line option found */
            ++ptr;
            switch (*ptr) {
                case 'c' :
                    /* we found "-c", so (ptr+1) points to something like "1,5" */
                    break;
                /* other cases */
                default :
                    /* unsupported command line option */
                    break;
            }
        }
0
 
abithCommented:
since '-' places in first position
if (argv[1][0] == '-')
...
0
 
Infinity08Commented:
>> if (argv[1][0] == '-')

That's what I had, no ?

        char *ptr = argv[1];
        if ('-' == *ptr)
0
 
tsuraiAuthor Commented:
What's the difference between strncmp and !strncmp?
0
 
Anthony2000Commented:
They are the same function, the ! means "not" or if the value returned is 0 (or false) then invert to non-zero (or true). In this case when strncmp returns 0 is means the strings being compared were equal so the if statement will need to evaluate to true when the strings are equal, so "!" was added to accomplish this.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now