您的位置:首页 > 编程语言 > C语言/C++

[C/C++] Using `getopt' in c/c++

2012-06-08 14:00 288 查看
Using `getopt' in c/c++

Could not get the real original version for the examples, list one of them first, usefully and well tested.

Examples

Example 1

// getopt_example.cpp

#include <unistd.h>

int main(int argc, char *argv[])
{
    int option = -1;
    char *addr, *port;

    while ((option = getopt (argc, argv, "i:p:")) != -1)
    {
         switch (option)
         {
         case 'i':
             addr = strdup(optarg);
             break;
         case 'p':
             port = strdup(optarg);
             break;
         default:
              /* unrecognised option ... add your error condition */
              break;
         }
    }

    /* rest of program */

    return 0;
}



See Also

Getopt - The GNU C Library - http://www.gnu.org/software/libc/manual/html_node/Getopt.html
The GNU C Library - http://www.gnu.org/software/libc/manual/html_node/index.html#Top
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐