简体   繁体   中英

getopt always returns 1

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM