Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-10-2011, 11:18 PM   PM User | #1
Krupski
Regular Coder

 
Krupski's Avatar
 
Join Date: Dec 2010
Location: United States of America
Posts: 502
Thanks: 39
Thanked 47 Times in 46 Posts
Krupski is on a distinguished road
Problem with "getopt" in Linux (GCC)

Hi all,

I have this little snippet of test code:

Code:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>

int main (int argc, char **argv)
{
    int c, w, aflag, bflag, cflag;
    c = w = aflag = bflag = cflag = 0;

    opterr = 0;

    while ((c = getopt (argc, argv, "abc")) != -1) {

        w++; /* debug count how many times we go through */

        switch (c) {

        case 'a':
            aflag = 1;
            break;
        case 'b':
            bflag = 1;
            break;
        case 'c':
            cflag = 1;
            break;
        case '?':
            fprintf(stdout, "Help shown here\n");
            return 1;
        default:
            fprintf(stdout, "Default: Should not see\n");
            return 1;
        }
    }

    fprintf(stdout, "While loop count = %d\n", w);

    for (c = 0; c < argc; c++) {
        fprintf(stdout, "argv[%d] = %s\n", c, argv[c]);
    }
    return 0;
}
Note the line in red... setting "opterr" to zero is supposed to suppress the built in error messages from getopt, yet it does not.

Running the code with an invalid option SHOULD simply print the help line, yet I get this:

root@michael:~/c-progs# ./getopt -x
./getopt: invalid option -- 'x'
Help shown here

The line in red isn't supposed to display.

I tried setting "opterr" to 1, -1 and leaving it out... no difference. What the heck? Anyone know?

Thanks.

-- Roger
__________________
"Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov
Krupski is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:50 AM.


Advertisement
Log in to turn off these ads.