简体   繁体   English

使用strtok组伪标记拆分字符串

[英]Split a String using strtok groups false tokens

I am trying to read a textfile line by line and to split each line into a token. 我试图逐行读取文本文件,并将每一行拆分为一个令牌。 But when I try to parse 但是当我尝试解析

D: 0 S: 0
push 0
prti
halt

I get an error in the first line. 第一行出现错误。 The result is 结果是

0: [D:] [0 S] [0] (3 Token)
1: [push] [0] (2 Token)
2: [prti] (1 Token)
3: [halt] (1 Token)

but I was expecting 但我期待

0: [D:] [0] [S:] [0] (4 Token) <--
1: [push] [0] (2 Token)
2: [prti] (1 Token)
3: [halt] (1 Token)

the second token should be 0 and the third should be S: but as you can see in the first result the second token is "0 s", the 3rd is 0 and there is no 4th token. 第二个标记应为0,第三个标记应为S:但是如第一个结果所示,第二个标记为“ 0 s”,第三个标记为0,没有第四个标记。

I added an example 我加了一个例子

#include <stdio.h>
#include <stdlib.h>

char *trimwhitespace(char *str) {
  char *end;
  while(isspace((unsigned char)*str)) str++;
  if(*str == 0) return str; 
  end = str + strlen(str) - 1;
  while(end > str && isspace((unsigned char)*end)) end--;
  end[1] = '\0';
  return str;
}

int main(void) {

    FILE * fp;
    char * line = NULL;
    size_t len = 0;
    ssize_t read;

    int tokencounter = 0;
    char *token;
    int linecounter;
    int address = 0;

    char *tokentemp;

    fp = fopen("test.asm", "r");
    if (fp == NULL) exit(1);

    while ((read = getline(&line, &len, fp)) != -1) {   

        token = strtok(line, " ");
        tokencounter = 0;

        printf("%d: ", linecounter);

        while( token != NULL ) {

            token = trim( token );
            printf( "[%s] ", token );
            tokencounter++;                
            token = strtok(NULL, line);           

        }

        printf("(%d Token)\n", tokencounter);

        linecounter++;

    }

    fclose(fp);
    if (line) free(line);
    exit(1);

}

Your second call to strtok uses wrong arguments. 您对strtok第二次调用使用了错误的参数。

Instead of 代替

token = strtok(NULL, line);

write

token = strtok(NULL, " ");

If you use line as parameter for the delimiter, then the second call will start with line starting with : , such that strtok will split upon : first (and not on 如果使用line作为定界符的参数,则第二个调用将从以:开头的line开始,这样strtok将在:分割: as you expected). 如您所料)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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