![](/img/trans.png)
[英]How to print an error message when a user enters two options using getopt in c programming instead of one?
[英]C programming - getopt
當我使用-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),並查看編譯器是否不會立即告訴您出了什么問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.