Thread: GETOPT() HElP!!!

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    11

    GETOPT() HElP!!!

    My piece of code adding HTML tags to input of user. FOR example

    4tab6tab7

    gives me

    `<tr><td>4<td><td>5<td><td>7<td></tr>`

    But sometimes I can have headings which means that I have to add (in our example) `<tr><th>4<th><th>5<th><th>7<th></tr>` (td is changed to th).
    I dont know how to make optional argument "-h num" (I run like "table -h3") which states how many lines at the beginning are "heading" lines; if this option is not present, it defaults to zero. I know that I have to use getopt(). But I dont know how to do it...Help me please....Thanks

    Code:
     #include <stdio.h>
        
        enum ctypes {
            CHAR, BLANK, LINE, NTYPES
        };
        
        int type(int c)
        {
            return  c == '\n' ? LINE :  c ==  '\t';
        }
        
        int main(void)
        {
            static const struct t {
                const char *fmt;
                int state;
            } trans[][NTYPES] = {
                { { "<tr><td>%c", 1 }, { "", 0 }, { "", 0 } },
                { { "%c", 1 }, { "</td>", 2 }, { "</td></tr>\n", 0 } },
                { { "<td>%c", 1 }, { "", 2 }, { "</tr>\n", 0 } }
            };
            const struct t *tp;
         int c, state;
        
        //    puts("<table>");
            for (state = 0; (c = getchar()) != EOF; state = tp->state) {
                tp = trans[state] + type(c);
                printf(tp->fmt, c);
            }
          //  puts("</table>");}

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by Manu Lakaster
    My piece of code adding HTML tags to input of user.
    I note that this looks almost identical to the code that Barney McGrew posted in post #16 of a previous thread that you started. Do you understand the code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    11
    yup, i do. I think I have to add while with getopt. But in the case I dont know how to show numbers of lines..

  4. #4
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Manu Lakaster View Post
    yup, i do. I think I have to add while with getopt. But in the case I dont know how to show numbers of lines..
    You do? Nobody understands Barney McGrew code apart from Barney McGrew.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by SirPrattlepod View Post
    Nobody understands Barney McGrew code apart from Barney McGrew.
    Even that is debatable at times .

    @Manu Lakaster:
    If you're waiting for another chunk of code you can copy-paste along Barney McGrew's state machine, you're out of luck. We don't generally hand out code here, you make an attempt, post it then we help you. The man page for getopt (link) has a good example on how to use the function, and Google will turn up several more.

    The advice:
    Use getopt() to parser the options (you need to change main to accept command line params, see our FAQ). Store the value you read in (e.g. the 3 in -h3) in a well-named int variable. Access that later when you need to know the value of the -h parameter.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Why do you need getopt when you declared your main function as int main(void)

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by Manu Lakaster
    yup, i do.
    Just to confirm: please explain to us, step by step, what the code does.

    Referring to your previous thread, you were struggling to implement the suggestions given. I finally consented to giving you a simple example with explanation. As such, I have a hard time believing that you actually have any idea what Barney McGrew's example is actually doing: it is simple, but it is also considerably different from your attempt and the details of what was suggested to you. If you would prove me wrong by showing that you understand the code, that would be great.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Looks to be another case of homework by successive approximation...
    c - GETOPT(). Help please - Stack Overflow

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

Tags for this Thread