简体   繁体   中英

how to token a full line input in c?

I need to parse a line from cmd that looks like this

"SOME WHITE SPACE" "var_name" "SOME WHITE SPACE" "var_value" "SOME WHITE SPACE"

i take the whole line with fgets:

fgets(input_buf,MAX_LINE_LENGTH,stdin);

and tried to tokenize like this:

sscanf(input_buf,"%s", var_buff);

sscanf(input_buf+strlen(var_buff),"%s", var_val_buff);

sscanf(input_buf+(strlen(var_buff)+strlen(var_val_buff)+2),"%s", rest_line_buff);

if (strlen(rest_line_buff) == 0)

    printf("error in usage\n");

I dont get the right values as the number of white spaces may vary. how can i tokenize the input line?

Use strtok , as shown in the sample program below:

#include <stdio.h>
#include <string.h>

int main ()
{
  char sampleInput[] ="foo bar foo1 bar1 foo2 bar2";
  char *token;
  char *whiteSpace = " \t\n\f\r\v";
  int isVariable = 1;

  token = strtok(sampleInput, whiteSpace);
  while (token != NULL)
  {
     if (isVariable) 
        printf("Variable = %s\n", token);
     else 
        printf("Value = %s\n\n", token);
     isVariable = isVariable ? 0 : 1;
     token = strtok(NULL, whiteSpace);
  }
  return 0;
}

Output:

Variable = foo
Value = bar

Variable = foo1
Value = bar1

Variable = foo2
Value = bar2

All functions from the scanf family aggregate and skip multiple whitespace characters by default:

sscanf(input_buf," %s %s", var_buff, var_val_buf);

I am not sure the leading whitespace in the format string is strictly necessary, but I am positive that it is correct event if there is no actual whitespace at the beginning of the input line.

使用扫描仪生成器: http : //en.wikipedia.org/wiki/Lexical_analysis#Lexer_generator或下载令牌生成器库。

It seems to natural to use strtok( ) or strtok_r( ) here. Why have you decided against it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM