当我使用 -f 时出现错误段错误,有什么想法吗? -f 选项用于选择输入文件。
struct list_names filenames;
list_names_init(&filenames, "filenames");
optind = 1;
while ((ch = getopt(argc, argv, ":hvf:")) != -1) //where getopt defined
{
switch (ch) {
case 'h':
printf("usage: ./hake [-h] [-v] [-f file]\n-h print help\n-v verbose mode; enable extra printing; can be repeated\n");
printf("-f file input filename; default is hakefile or Hakefile");
break;
case 'v':
verbose = 1;
break;
case 'f':
f_flag++;
list_names_append_from_file(&filenames, optarg);
printf("Read_file%s\n",optarg);
read_file(optarg);
break;
最佳答案
不知道 list_names_append_from_file
是如何定义的,很难回答您的问题。但是,段错误意味着您几乎可以肯定将错误类型或数量的参数传递给函数,或者您弄乱了指针。我建议使用 -Wall -Werror 重新编译(假设您使用的是 gcc),看看编译器是否不会立即告诉您出了什么问题。
关于C 编程 - getopt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9440898/