C++ 中 char* 中的子字符串,行尾带有 strtok

sub string in char* in c++ with strtok to the end of the row

本文关键字:strtok 字符串 char C++      更新时间:2023-10-16

我尝试在 c++ 中对一个输入 char* 的字符串进行分类这是我的代码

void DBManager::printMatched(char* line, char* fileName)
{
    line = strtok(line,"Show");
    char* teamAName = strtok(line," ");
    char* teamACity = strtok(NULL,"-");
    char* teamBName = strtok(NULL," ");
    char* teamBCity = strtok(NULL,"n");
}

这是行中的文本"Show abcde fghij - klmnop qrstu"

这是变量数据:

 teamAName = abcde
 teamACity = fghij
 teamBName = klmnop
 teamBCity = qrs

我怎样才能修复我需要切入行的团队BCity。

我在Linux系统上工作。

我认为这就是你想要的。 鉴于您的示例,不完全确定。 它显然可以改进,也许您想使用更多的 c++ish 工具?

void DBManager::printMatched(char* line, char* fileName)
{
    char* linecpy   = strdup(line);
    char* dummy     = strtok(linecpy," ");
    char* teamAName = strtok(NULL," ");
    char* teamACity = strtok(NULL," ");
          dummy     = strtok(NULL," ");
    char* teamBName = strtok(NULL," ");
    char* teamBCity = strtok(NULL," n");
    printf("teamAName %sn", teamAName);
    printf("teamACity %sn", teamACity);
    printf("teamBName %sn", teamBName);
    printf("teamBCity %sn", teamBCity);
    // do something with strings here?
}