Thread: Using getopt(3c) correctly

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    18

    Using getopt(3c) correctly

    Hello all, I'm having trouble with getopt(3c) scanning in characters from command line. Specifically I want to see if two occurrences of the same character are present within the argument.

    So for example ./program -abcdd where dd occurs twice.

    Currently, I have the scanning as:

    Code:
       int x = 0;
       for (;;) 
       {
          int option = getopt (argc, argv, "ab:cd");
          
          if (option == EOF) break;
          switch (option) 
          {
             case 'a': do something;
                       break;
             case 'b': do something;
                       break;
             case 'c': do something;
                       break;
             case 'd': x++;
                       break;
             default : exit;
          }
       }
    My logic is that if x occurs twice, then it'll be incremented twice and i can use if ( x==1) or if ( x==2). But this doesn't seem to be working.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I like to use a small int array. As the letter is read, increment the small[letter]. Now you know the distribution, entirely, including doubles.

    I don't like incrementing a variabled called 'x', for a duplicate letter of 'd'. You'll need 0-127 for all the low ascii char values, and 0-255 for ALL the ascii char set. Locale sets may be larger, depending on the language they use and their character sets.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getopt
    By Dr Saucie in forum C Programming
    Replies: 1
    Last Post: 03-11-2010, 12:00 PM
  2. getopt()
    By sugumar.tr in forum C Programming
    Replies: 1
    Last Post: 07-09-2009, 04:52 AM
  3. What is getopt?
    By samus250 in forum C Programming
    Replies: 2
    Last Post: 03-21-2008, 11:58 AM
  4. getopt help
    By wuzzo87 in forum C Programming
    Replies: 12
    Last Post: 04-09-2007, 03:16 AM
  5. getopt()
    By threahdead in forum Linux Programming
    Replies: 1
    Last Post: 04-03-2003, 08:28 AM