Using getopt() more than once?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Using getopt() more than once?




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Using getopt() more than once?  
Kenny McCormack


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-10-07 12:19 AM

(since it uses global variables to track its internal state), getopt()
seems, by design, to be only usable once.  But it is not too hard to
imagine situations where you'd want to call it multiple times in a given
program.  Is this possible (in any at least psuedo-portable way) ?

(answering my own question), I have found that setting optind to 1,
seems to work.  That is, I do:

#include <getopt.h>
..
do normal getopt() stuff;
..
optind = 1;
do getopt() again;






[ Post a follow-up to this message ]



    Re: Using getopt() more than once?  
Loic Domaigne


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-11-07 12:18 AM

Hello Kenny,

> (since it uses global variables to track its internal state), getopt()
> seems, by design, to be only usable once.  But it is not too hard to
> imagine situations where you'd want to call it multiple times in a given
> program.  Is this possible (in any at least psuedo-portable way) ?
>
> (answering my own question), I have found that setting optind to 1,
> seems to work.  That is, I do:
>
>         #include <getopt.h>
>         ...
>         do normal getopt() stuff;
>         ...
>         optind = 1;
>         do getopt() again;

Seems to be the way to go... You may want to reset opterr and optopt
as well.

Cheers,
Loic.






[ Post a follow-up to this message ]



    Re: Using getopt() more than once?  
Roger Leigh


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-11-07 12:20 PM






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:46 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register