【发布时间】:2018-04-10 17:39:08
【问题描述】:
您好,我对编码很陌生,并试图找出这个 getopt 不起作用的原因。我的编译器抱怨“i:o:”
错误 C2664 'int getopt(int,char **,char *)': 无法将参数 3 从 'const char [5]' 转换为 'char *'
int main(int argc, char *argv[])
{
int opt;
while ((opt = getopt(argc, argv, "i:o:")) != -1)
{
switch (opt)
{
case 'i':
printf("Input file: \"%s\"\n", optarg);
break;
case 'o':
printf("Output file: \"%s\"\n", optarg);
break;
}
}
return 0;
}
这很奇怪,因为当我阅读 getopt 时,我看到了“选项参数是一个字符串,它指定了对该程序有效的选项字符。”
【问题讨论】:
-
第三个参数必须是可写的,字符串字面量是不可写的。这可能是您正在使用的 getopt 库中的“代码异味”。
-
您使用的是哪个运行时?我看到
int getopt(int argc, char * const argv[], const char *optstring); -
我建议 Boost.Program_options 优于
getopt。 -
@FredLarson 试过了。没有留下深刻印象。
-
我不知道 Visual Studio 库中有任何
getopt函数 - 您包含哪个标头来获取它?