2014-03-02 26 views
0

我编写了代码以查找单引号或双引号,一系列字符或“|”的开始和结尾。然后将这些值放入数组中。出于某种原因我不断收到段错误。此外,之后,如何显示表格以查看它是否正常工作?解析字符串并将令牌的索引放入表中

int parsing(const char *commandline, int array[]) 
{ 
    int number= 0; 
    int index = 0; 
    while(commandline[index]!= '\0'){ 

     /* when you have spaces*/ 
     if (isspace(commandline[index])) 
      index++; 

     /* when you have double quotes*/ 
     if(commandline[index] == '"'){ 
      printf("running"); 
      array[number] = index; 
      number++; 
      index++; 
      while(commandline[index] != '"' || '\0') 
       index++; 
      if(commandline[index] == '\0') 
       return printf("ERROR: Quote not ended"); 
      array[number]= index; 
      number++; 
     } 

     /* when you have single quotes*/ 
     if(commandline[index] == '\''){ 
      array[number] = index; 
      number++; 
      index++; 
      while(commandline[index] != '\'' || '\0') 
       index++; 
      if(commandline[index] == '\0') 
       return printf("ERROR: Quote not ended"); 
      array[number]= index; 
      number++; 
     } 


     /* when you have |*/ 
     if (commandline[index] == '|') 
     { 
      array[number] = index; 
      number++; 
     } 


     /* when you have only letters*/ 
     if(isalnum(commandline[index])){ 
      array[number] = index; 
      index++; 
      number++; 
      while (isalnum(commandline[index])) 
       index++; 
      array[number] = index; 
     } 

    } 


    return *array; 
} 

编辑:添加错误检查

+0

如果有什么最后一个字符是一个空间?或者是第二个''“''?或者如果字符串没有*第二个''”''? –

+0

我还没有这样做,我试图看看是否这么多工作 – user3288169

+0

使用gdb来检查段错误的行。要显示你的数组,使用一个简单的for循环,并用printf显示每个条目。 – gnidmoo

回答

0

我发现多事情,以改善,增加他们的意见:

int parsing(const char *commandline, int array[]) 
{ 
    int number= 0; 
    int index = 0; 
    while(commandline[index]!= '\0'){ 

     /* when you have spaces*/ 
     if (isspace(commandline[index])) { 
      index++; 
      continue; // So that if you are at the end of the string 
         // you don't segfault. 
     } 

     /* when you have double quotes*/ 
     if(commandline[index] == '"'){ 
      printf("running"); 
      array[number] = index; 
      number++; 
      index++; 
      // || '\0' just does nothing. Do: 
      while(commandline[index] != '"') { 
       if(commandline[index] == '\0') { 
        // returning printf sounds silly. printf returns the number 
        // of character written. 
        // Also: Add \n to your printf. 
        printf("ERROR: Quote not ended\n"); 
        return -1; 
       } 
       index++; 
      } 
      array[number]= index; 
      number++; 
      index++; 
      continue; // Same as for when you have spaces. 
     } 

     /* when you have single quotes*/ 
     if(commandline[index] == '\''){ 
      array[number] = index; 
      number++; 
      index++; 
      // Ditto there. 
      while(commandline[index] != '\'') { 
       if(commandline[index] == '\0') { 
       printf("ERROR: Quote not ended\n"); 
       return -1; 
       } 
       index++; 
      } 
      array[number]= index; 
      number++; 
      index++; 
      continue; // here also. 
     } 

     /* when you have |*/ 
     if (commandline[index] == '|') 
     { 
      array[number] = index; 
      number++; 
      index++; // move to next character. 
      continue; 
     } 


     /* when you have only letters*/ 
     if(isalnum(commandline[index])){ 
      array[number] = index; 
      index++; 
      number++; 
      while (isalnum(commandline[index])) 
       index++; 
      array[number] = index; 
      continue; 
     } 
     // And if you get here, you have neigher an alphanum nor a | nor a " 
     // nor a ', what do you want to do ? 
     index++; 
     continue; 

    } 


    return *array; 
} 
+0

这很棒。摆脱了设置的错误。我确实添加了继续。现在我唯一的问题是获得正确的索引 – user3288169

+0

在你的代码中添加一些printf来'看'它在做什么。 gdb也是一种选择,但通常比printfs更难使用;) –

+0

出于某种原因,它说如果你试图用单引号或双引号运行代码,引用不会结束 – user3288169

相关问题