What is the alternative to the getopt function in Windows C ++?

Below code I am using Posix C:

while ((opt = getopt(argc, argv, "a:p:h")) != -1)

      

How can I port this code to Windows C ++ using an alternative function?

thank

+3


source to share


2 answers


If you were looking, you would find another thread that will distribute some of the compatibility libraries for getopt, among other implementations, for Windows based systems.

getopt.h: Compiling Linux C Code on Windows

On another note, you can always use the va_arg, va_list, va_start and va_end functions to handle arguments.



/* va_arg example */
#include <stdio.h>      /* printf */
#include <stdarg.h>     /* va_list, va_start, va_arg, va_end */

int FindMax (int n, ...)
{
  int i,val,largest;
  va_list vl;
  va_start(vl,n);
  largest=va_arg(vl,int);
  for (i=1;i<n;i++)
  {
    val=va_arg(vl,int);
    largest=(largest>val)?largest:val;
  }
  va_end(vl);
  return largest;
}

int main ()
{
  int m;
  m= FindMax (7,702,422,631,834,892,104,772);
  printf ("The largest value is: %d\n",m);
  return 0;
}

      

Link: http://www.cplusplus.com/reference/cstdarg/va_list/

+1


source


Microsoft has provided a nice implementation (as well as some other helpers) inside the IoTivity open source project that you could reuse (pending any licensing requirements you might have):

Look at the "src" and "include" directories here for "getopt.h / .c".



https://github.com/iotivity/iotivity/tree/master/resource/c_common/windows

+1


source







All Articles