I want to use getopt
to get the argument list of my console tool.
When I call my tool like below getopt
returns always 1
and doesn't mactch any switch/case
.
Am I doing something wrong?
mytool -f farg -d darg
int
main(int argc, char** argv) {
int c;
while((c = getopt(argc, argv, "f:d:h") != -1)) {
switch(c) {
case'f':
break;
default:
break;
}
}
while((c = getopt(argc, argv, "f:d:h") != -1))
It works like
c = (getopt(argc, argv, "f:d:h") != -1)
Well, that is 1 always because the result of the comparison is stored to c
. In your case the getopt
does not return -1
. If it returns -1
then c
would be 0
. The fix is
while((c = getopt(argc, argv, "f:d:h")) != -1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With