LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   Trying to make getopt work (C). (http://www.linuxquestions.org/questions/slackware-14/trying-to-make-getopt-work-c-4175475254/)

stf92 08-29-2013 07:54 PM

Trying to make getopt work (C).
 
Hi: See this?

Code:

semoi@server:~$ cat t07.c
#include <stdio.h>

int option;


main(int argc, char** argv){
        while((option= getopt(argc, argv, "vg")) != -1){
        switch(option){
                case 'v':
                        printf("v\n");
                        return;
                case 'g':
                        printf("g\n");
                        return;
        }
        }
}
semoi@server:~$ gcc -O2 -o t07 t07.c
semoi@server:~$ ./t07 v
semoi@server:~$ ./t07 g
semoi@server:~$

It should have printed 'v', then 'g'. What's wrong?

stf92 08-29-2013 07:59 PM

I made a fool of myself. It's './t07 -g' I should have typed. Sorry.

T3slider 08-29-2013 08:00 PM

getopt looks for options beginning with a hyphen (-). The following should work:
Code:

$ ./t07 -v
$ ./t07 -g

[edit] Too late. ;) [/edit]

perbh 08-29-2013 08:07 PM

yanno - I've been programming exclusively in 'c' for 20 years - and I still make mistakes like that!!

stf92 08-29-2013 09:42 PM

Yes. But sometimes I find it easier to send a post rather than doing a little thinking, which is bad. Only sometimes.

a4z 08-30-2013 02:00 AM

that is an interesting effect which I also know from my self,
having a question, write the question to a forum, post it, and as soon as you hit the post button the answer pops up in your brain.

stf92 08-30-2013 03:01 AM

The problem is that there are things one only considers as a means. Having the end in mind, I gets impatient and I want to get over it as soon as possible. After all, it's all about we people fond of open source getting in touch with one another, and learning by the way, a thing I love.

GazL 08-30-2013 03:36 AM

If you make a habit of posting trivial questions that can be solved by a google search, reading the man-page, or even just a few seconds of thought then people will soon tire of you and add you to their ignore list. It's something to keep in mind before you hit that 'submit' button.

Posting to the wrong forum won't help either: what did this question have to do with Slackware?

stf92 08-30-2013 03:45 AM

You think LQ is the oracle of Delphi? If you do, you are wrong. I am prone to believe it's more like a place where people gets in touch with people who know, devoided of solemnity.

And was not completely sincere when speaking about means and ends. Sometimes the means are even more interesting then the end.

GazL 08-30-2013 04:04 AM

Take the warning for what it is, or ignore it if you like. It's no skin off my nose if you end up on ignore lists.


All times are GMT -5. The time now is 01:50 PM.