2012-02-25 48 views
0

当我使用-f得到错误分段错误时,有什么想法? -f选项用于选择输入文件。C编程 - getopt

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; 
+0

可能不是问题出在你的'list_names_append_from_file'方法中。 – Kevin 2012-02-25 03:30:24

回答

3

不知道list_names_append_from_file是如何定义的,它很难回答你的问题。但是,分段错误意味着您几乎可以肯定地将错误的类型或数量的参数传递给函数,或者您正在搞乱指针。我建议用-Wall -Werror重新编译(假定你正在使用gcc),看看编译器是不是立即告诉你什么问题。

+0

看着我的代码,你的权利,我传递错误的参数数量的函数。我被困在如何同时传递两个文件到列表中,是否有可能这样做? – 1990Mustafa 2012-02-25 16:22:23